How and when to use memory allocation? (new / malloc)

Started by
11 comments, last by vaceX 21 years ago
Today, after using a lot of memcpy(), strcpy(), strcat(), char[] and char *, I realized that something wasn''t right. Look at this piece of code, and tell me if there is anything -wrong- in it (that has something to do with memory usage): char recieve_temp[512] memset(recieve_temp, ''\0'', 512); int recieve_size = recv(wParam, recieve_temp, sizeof(recieve_temp),0); char second_recieve_temp[512]; memcpy(second_recieve_temp, recieve_temp+6, recieve_size-6); And btw... when am I suppoused to use new / malloc?
Advertisement
quote:Original post by vaceX
memcpy(second_recieve_temp, recieve_temp+6, recieve_size-6);


why +6, -6? (Not sure myself, just asking)

quote:Original post by vaceX
And btw... when am I suppoused to use new / malloc?


when you need memory from your RAM?


.lick
One thing I notice is that this line:

int recieve_size = recv(wParam, recieve_temp, sizeof(recieve_temp),0);

should be:

int recieve_size = recv(wParam, recieve_temp, sizeof(recieve_temp)-1,0);

If you are recieving text. Then set the char at recieve_size to null.
quote:Original post by Pipo DeClown
why +6, -6? (Not sure myself, just asking)

I don''t want the first 6 chars, and therefore I need to copy 6 less chars.


quote:Original post by Pipo DeClown
when you need memory from your RAM?

But when do I need more memory? I don''t get this at all...
There are circumstances in a program where you can''t predict how much memory you will need at compile time, e.g. a program which loads pictures. You don''t know how big the picture will be, so you will need to allocate at run time.
Or when you have:
char buffer[1000000]; // 1mb 


and you don't want your exe to me > 1mb, you do this:
char* buffer = new char[1000000]; // this just takes 1mb from your RAM 


then when you don't need it:
delete[] buffer;     



.lick



[edited by - Pipo DeClown on May 1, 2003 9:44:32 AM]
char recieve_temp[512];

you dont need to allow memory to that because its not a pointer.
[512] implies reserving 512 byte of memory for that variable.


You would allow memory if you were using a pointer instead, for example char* receive_temp;

then you would do :
receive_temp= new char[512]; in c++
or
receive_temp= malloc(512); in C

You can use malloc in c++ as well.
quote:Original post by Pipo DeClown
Or when you have:
char buffer[1000000]; // 1mb  


and you don''t want your exe to me > 1mb, you do this:


Bullshit. That won''t make the executable larger than 1mb. And you HAVE TO delete after new..
Look at this thread for questions about memory allocation...

Regards,
Jeff

[edited by - rypyr on May 1, 2003 9:59:25 AM]
Let''s suppose you''re coding an old-school RPG... The player runs into an encounter.

Now, you have a structure for your enemies. The structure is about 15k in size, nothing too serious. But the thing is, you don''t know how many enemies may pop up in that encounter. Could be one, could be ten...

Two solutions. One, you create an array of 10 enemies, and only use the ones you need. That''s 150k. Nothing too serious, but in other cases it could be pretty bad (if you store graphics, for instance, and have a meg or two-large structures).

The other solution is to allocate memory for just what you need. Let''s suppose there''s just one encounter set that has 10 enemies and the second-largest is 4 enemies. Or that you have a fight where a boss summons little minions. You could simply create the enemies dynamically by reserving memory for them with malloc.

While you''re at it, why not simply reserve what you need for your structure? If an enemy can''t cast spells, why give it a list of up to 20 possible spells to use in the battle? Or if you store the enemy''s sprite in the structure, why reserve 128x256, the size of the largest enemy, when most are only 32x32?

That''s what malloc is for. And you also have to delete malloced memory otherwise you''ll cause a memory leak and have reserved, unused memory laying about in RAM after your program quits.

This topic is closed to new replies.

Advertisement