Skip to main content
gamedev.net
🔒 Locked

another way for malloc ?

Started by Lucien Apr 9, 2002 at 6:42 AM 7 replies 200+ views
Original Post
Lucien
Lucien
Hello !!!!! How is it going ???? well I have got a problem :s I use malloc to allocate memory space but I don''t know, probably I allocated too much because program stop allocating (returning NULL) and often bugs when I am writing into an array. Do you know another way to do same as malloc ? I need to find a good way to store dynamic datas. That I can read quickly and easily. Thanks a lot for reading ! I hope you could help me a little, because I am really bored with that. Anyway, have A GOOD DAY ! you can contact me on MSN, the_real_dj_tiesto@hotmail.com :>
Dean Harding
Dean Harding
new and malloc work exactly the same, the only difference being that new will call an object''s constructor after the memory is allocated. In most cases, they''re both implemented with the same OS-specific functions. Well, they''ll often use a different heap, hence you can''t free() a new''d structure, etc...

Anyway, that''s not the point... the point is that you''re more likely doing something wrong. Check to make sure how much you''re actually trying to allocate. For example, since malloc takes an unsigned integer for it''s parameter, giving it a negative number will result in trying to allocate some 2^31 bytes...


codeka.com - Just click it.
Stoffel
Stoffel
..or you''ve used up all the memory on the system.
SabreMan
SabreMan
quote:
Original post by Stoffel
..or you''ve used up all the memory on the system.

He''s more likely used up all the memory available to the process.
cgoat
cgoat
What AP said is probably something worth trying, though you don't even have to use malloc at all for it. Here's a more thorough explanation of why, with examples.

Lets say you have a structure that takes up 1024 bytes:


struct big_struct
{
char str1[256];
char str2[256];
char str3[256];
char str4[256];
};


If you want to allocate 1000 of them as an array, you need 1024000 bytes of CONTIGUOUS memory space. That's a whole megabyte of memory in one chunk. This could cause problems if you don't have lots and lots of memory. So what you do instead is to first allocate an array of 1000 pointers to your objects (4000 bytes):


big_struct **the_list = new big_struct*[1000];


Now you go through the list of pointers and allocate a structure for each one (1024 bytes each structure):


for(int x=0; x<1000; ++x)
{
the_list[x] = new big_struct;
}


This may work where the original way failed since the largest chunk of memory you ask for is 4k, a much more manageable number.

You can access each struct like so:


*(the_list[x]); // The actual structure
the_list[x]->str1; // The element in the structure


For cleanup, you do the opposite order:


for(int x=0; x<1000; ++x)
{
delete the_list[x];
}
delete[] the_list;


Voila!

(Fixed formatting)

[edited by - cgoat on April 9, 2002 1:19:23 PM]
Fruny
Fruny
quote:
Original post by Anonymous Poster
Another solution is to use the STL. Look at the vector template.



Memory in a std::vector is guaranteed to be contiguous too.
Use a std::deque.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
jenova
jenova
if you are allocating and deallocating memory constantly, you might be running out of contiguous memory and that''s why you are getting that error. if that''s the case you then you might want to look into re-using your memory.

if you''re just running out of memory then you might want to look intoVirtualAlloc.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
cgoat
cgoat
Lucien: You are free()ing the memory after you''re done, right?

I know, it''s like asking if you plugged in the computer, but a lot of people don''t realize you have to actually undo what you do in a C/C++ program (this isn''t Java with garbage collectors).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.