VERY basic memory question
-
I have a really simple question which I cannot seem to find the answer to... If I allocate memory like this:
char * pArray = (char*)malloc(123);
Then I free it with:free(pArray);
And the same with:char * pArray = new char[123];
Anddelete[] pArray;
But how would I free an array created like:WCHAR pwszRealPath[MAX_PATH];
The best answer I can find is that I don't but I would like to know why. -
I have a really simple question which I cannot seem to find the answer to... If I allocate memory like this:
char * pArray = (char*)malloc(123);
Then I free it with:free(pArray);
And the same with:char * pArray = new char[123];
Anddelete[] pArray;
But how would I free an array created like:WCHAR pwszRealPath[MAX_PATH];
The best answer I can find is that I don't but I would like to know why.__DanC__ wrote:
how would I free an array created like: WCHAR pwszRealPath[MAX_PATH];
such an array is created on the stack, and not on the heap. so basically, no need to free it ;) it will be destroyed when going out of scope, of at program termination if static/global...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
__DanC__ wrote:
how would I free an array created like: WCHAR pwszRealPath[MAX_PATH];
such an array is created on the stack, and not on the heap. so basically, no need to free it ;) it will be destroyed when going out of scope, of at program termination if static/global...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Thanks, that is what I thought would be the case but wanted to clarify in case I go causing any memory leaks!
to be very clear here is what happens with this code:
char* pArray = new char[123];
a variable pArray of type pointer to char is created on the stack. then, a block of 123 chars is allocated on the heap. the address of the just allocated memory is stored into the pointer variable you have on the stack (pArray). so, if you put such a code into a scope, and if you don't free the memory allocated before going out of scope, the variable on the stack is destroyed, but not the memory block on the heap. so you just loose the reference (the address) to your allocated memory on the heap, hence the memory leak...
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Thanks, that is what I thought would be the case but wanted to clarify in case I go causing any memory leaks!
To make things even more simple, there's only one little rule to remember: everything that is allocated with new should be deleted using delete; everything that is allocated with new...[] should be deleted using delete[] and everything that is allocated with malloc should be deleted with free. For the rest, you don't have to care (and Toxcct explained that a bit more in details).
Cédric Moonen Software developer
Charting control [v1.4] OpenGL game tutorial in C++ -
To make things even more simple, there's only one little rule to remember: everything that is allocated with new should be deleted using delete; everything that is allocated with new...[] should be deleted using delete[] and everything that is allocated with malloc should be deleted with free. For the rest, you don't have to care (and Toxcct explained that a bit more in details).
Cédric Moonen Software developer
Charting control [v1.4] OpenGL game tutorial in C++But what about GlobalAlloc()? (Sorry, couldn't resist. :) )
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
But what about GlobalAlloc()? (Sorry, couldn't resist. :) )
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
There is a verry good discussion here http://www.codeguru.com/forum/showthread.php?t=55854[^]
-
There is a verry good discussion here http://www.codeguru.com/forum/showthread.php?t=55854[^]
I was making a joke.
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke