Archived

This topic is now archived and is closed to further replies.

vaceX

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

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
quote:
Original post by vaceX
And btw... when am I suppoused to use new / malloc?

Most important difference between malloc and new is that malloc is C library function. new is a opperator.
So the problem ocure when You allocate classes.
cList *list = (cList*) malloc(sizeof(cList)); // BAD
cList *list = New Clist; // good

Operator new will call cList construktor, malloc will not!.
So every time You will alloc place for class you shold use new, for rest You can use malloc.

[edited by - Estor on May 9, 2003 9:43:16 AM]

Share this post


Link to post
Share on other sites
quote:
So every time You will alloc place for class you shold use new, for rest You can use malloc.


''for rest You can use malloc'' - I assume you mean PlainOldData and not just struct. Structures are classes too and have constructors/destructors that might need calling.

Share this post


Link to post
Share on other sites
quote:
Original post by Estor
Operator new will call cList construktor, malloc will not!.
So every time You will alloc place for class you shold use new, for rest You can use malloc.



If you''re coding in C++, I can''t imagine a single appropriate time to call malloc()/free(), unless you''re using a C library or something like that.

Share this post


Link to post
Share on other sites