Thread: Program taking up 100% of everything

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    59

    Program taking up 100% of everything

    I made this little program that prints out a random line from a file, but whenever I run it (under Windows) the CPU jumps to 100% and it takes up a lot of RAM, and then it ends without outputting anything. Under BSD (FreeBSD, from a shell account) it runs out of memory \very\ quickly. I have yet to try it under Linux but I would expect the same results. I have no idea why it is doing that, and I don't know where else to turn.

    The code is attached since posting it messed up the tables because I indent with tabs.

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Well, for starters you're calling the function count() with a char* as parameter although it accepts a string.
    [edit]: Disregard that I think it does an implicit cast which makes the argument to a string. Haven't done C++ in a while.

    You can test the program where it crashes by printing some line and then moving it around and you'll know you have found the crash point when the output is not printed to the screen anymore.
    Last edited by OnionKnight; 02-08-2006 at 08:42 PM.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    getline's default delimiter is a newline. it will read up to the newline and put all that into the string. getline also removes the newline from the stream, so it will not showup in your string. Also you are returning a pointer to a local variable of random. the vector goes out of scope when your function returns. These may not be the solution, but they are problems.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Ah, ok, I was wondering about all those pointers and the scope of them in random.

    Regaurding the getline-removing-newline-char, what alternative would I use, or how would I use getline to keep the \n?

    Thanks for all the help so far, I'll start fixing the problems pointed out.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    you could move the vector to count. Each line read in by getline could be added to the vector. If you need to edit the strings inside random, you could pass the vector by reference, or const reference if you don't.

    edit: Also, if you just want to get a random string from the vector you could do
    Code:
    vectorname[rand() % vectorname.size()]
    I've also seen something called random_shuffle talked about before that could randomize the vector. I haven't dealt with it myself, and don't know if it's standard.
    Last edited by Syneris; 02-08-2006 at 09:22 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I've also seen something called random_shuffle talked about before that could randomize the vector.

    I don't have time to look at the attachment right now, but random_shuffle is standard and can be used with a vector: std::random_shuffle(vec.begin(), vec.end()); You should #include <algorithm>.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Limiting another program from taking to much of system
    By adr in forum Windows Programming
    Replies: 9
    Last Post: 07-02-2007, 01:47 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. Stock Taking program
    By C Babe in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2003, 07:40 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM