Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 I'm trying to add lines into the middle of a text file?
Index -> Programming, C++ -> C++ Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
canmovie




PostPosted: Sun Jan 20, 2013 1:59 am   Post subject: Re: RE:I\'m trying to add lines into the middle of a text file?

DemonWasp @ Sun Jan 20, 2013 1:41 am wrote:
Each card can be represented by a value in the range 0-53 (8 bits). You can represent an entire hand in 5 bytes. Then if you allocate even 1MB of memory, you can store nearly 210,000 hands.

Even if you do store 75,000 hands at 10 ints = 40 bytes (assuming 32-bit integer, though that's not guaranteed with C/C++), that's still only 3MB of memory. Your computer probably has over 1000 times that.

Even your 'original' struct is only 25 bytes per card, which is less than 9MB of storage total.

I seriously don't think you're running out of memory.

There are a lot of other things that could cause your program to crash with the larger data set. Are you allocating any arrays of fixed size? Are you using any for loops over fixed ranges? Are you leaking heap-allocated strings? Are you multiple-delete or multiple-freeing? We can't help you answer that question without your full source code.


I think the default int is 4 bytes - i was using 6*4 (6 because I need to leave an empty spot in each hand for other reasons) = 24 bytes to store the values of the cards and 24 bytes to store the suits of the cards for each hand.
That is 48 bytes for each hand.
48 bytes * 75.000 = about 3.4 mb

Hmm you were right, that's nowhere close to running out of memory. (By the way, now I have a much better understanding of how big memory actually is, holy shit.)
I wonder why it didn't work before I changed from int to unsigned char, but it must have SOMETHING to do with less memory, otherwise what difference would changing from int to unsigned char make?
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Sun Jan 20, 2013 3:06 am   Post subject: RE:I\'m trying to add lines into the middle of a text file?

Changing int to unsigned char could make a world of difference without changing memory consumption. It's possible that it's just masking some kind of non-obvious bug in your code that would crop up if you ever increased the number of hands again.

The only other thing to mention is that if you declared all of those arrays on the stack, then you could easily run into problems. The stack has a much lower allocation limit than heap allocation (using malloc or new) does.
canmovie




PostPosted: Sun Jan 20, 2013 12:36 pm   Post subject: Re: RE:I\'m trying to add lines into the middle of a text file?

DemonWasp @ Sun Jan 20, 2013 3:06 am wrote:
Changing int to unsigned char could make a world of difference without changing memory consumption. It's possible that it's just masking some kind of non-obvious bug in your code that would crop up if you ever increased the number of hands again.

The only other thing to mention is that if you declared all of those arrays on the stack, then you could easily run into problems. The stack has a much lower allocation limit than heap allocation (using malloc or new) does.


How can I specify where to declare the arrays?
What's the difference between a declaration of those arrays on the stack and a declaration of those arrays on memory?
Insectoid




PostPosted: Sun Jan 20, 2013 1:17 pm   Post subject: RE:I\'m trying to add lines into the middle of a text file?

The stack is part of memory.

The heap is memory designated for dynamically allocated data (using malloc(), for example). This data is only freed when you manually free it (using free()) or when the program quits.

The stack is for 'regular' variables. This memory is freed when it goes out of scope. If you declare a variable inside a function, that integer gets freed when the function finishes execution.

c:

int main(){
    int* foo; //foo is on the stack
    foo = (int) malloc (sizeof (int)); //foo points to an int on the heap
    char baz = "a"; //this is on the stack.
}


If you used, say, a tree, you could fit thousands of hands in it and it could even sort itself.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 19 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: