Skip to main content
gamedev.net
🔒 Locked

Memory Leak

Started by OleKaiwalker Mar 3, 2004 at 7:31 AM 8 replies 500+ views
Original Post
OleKaiwalker
OleKaiwalker
Hey, Why is this code not causing any memory leak? // Exampel for my init function int *pInt = new int[size]; // Then the resizer int *pTmpInt = new int[++size]; memcpy(pTmpInt, pInt, size - 1]; delete[] pInt; pInt = pTmpInt <-- Since I''m not deleting the temp array, shouldn''t it give me a memory leak??? pInt[count++] = object; Do you see my point??? Another question. Is this a good way to resize an array?
superdeveloper
superdeveloper
Im with AP on this one... why would you suspect that there SHOULD be a leak?

You are setting your main pointer to the next larger array, and deleting the previous (smaller) array. Is this a trick question?
fractoid
fractoid
Ummmm... the memcpy(), shouldn't that be:
memcpy(pTmpInt, pInt, (size - 1) * sizeof(int) );
(unless sizeof(int) == 1)?

[edit: other than that, seems like an OK way to resize an array to me. Possibly use std::vector instead for ease and robustness and slight speed increase (efficiency of growing by more than 1), but no big.]

[edited by - fractoid on March 3, 2004 8:50:09 AM]
NatasDM
NatasDM
quote:
Original post by OleKaiwalker
Another question. Is this a good way to resize an array?


Well, no. It causes extreme memory fragmentation of the heap (assuming you would allocate other resources).

So: Bad idea for memory constrictive devices, such as gaming consoles, PDAs, cellphones, and hand held applications in general.

I''d say learn STL after your experiments. Sure, doing this stuff will help you better understand memory and pointers, but STL is the wheel that you shouldn''t reinvent. It is a set of containers that is faster and better than you could spend time on.

Download STLPort and upgrade your compiler''s STL. (Especially if you use MSVC++ 6)
OleKaiwalker
OleKaiwalker
Okay...

How does STL manange it''s allocated memory, making it so good?
emilk
emilk
quote:
Original post by OleKaiwalker
Okay...

How does STL manange it''s allocated memory, making it so good?


The point isn''t how it manages it''s memory.
The point is you don''t have to.
dmikesell
dmikesell
quote:
Original post by OleKaiwalker
Okay...

How does STL manange it''s allocated memory, making it so good?


std::vector does the resizing for you, and since it''s a template class you can create vectors of any type with it, instead of re-writing your code for float *, char *, SomeObject *, etc.



--
Dave Mikesell Software
fractoid
fractoid
Specifically std::vector doubles it''s size each time it runs out of space, so the number of times it needs to grow is O(log(n)) for n elements added one at a time, rather than O(n). This means (as pointed out here) that adding a string of items drops from quadratic to linear, which is A Good Thing.

Actually, it just has to grow by a percentage of it''s current size - everyone seems to use 100%, since doubling leads to power-of-two vector capacities, which programmers seem to inherently like.

And besides all that, the main thing is that as emilk says, std::vector handles all the reallocation and stuff for you, which is Another Good Thing.
Spudder
Spudder
You only have a memory leak when all pointers to dynamically allocated memory fall out of scope before that memory is freed, in your example you preserve a pointer to the memory by assigning pInt to pTempInt so no memory leak as later on you can call delete [] pInt to free the memory originally allocated to pTempInt.

Topic Locked

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

Sign in to reply to this topic.