View Full Version: Memory Allocation

C++ Learning Community > C++ in General > Memory Allocation


Title: Memory Allocation


mikawo - August 27, 2003 01:33 AM (GMT)
I'm using MSVC6. When I allocate around 64k (~57275 bytes to be more precise) of memory and take the necessary steps to free it, reports show that the memory wasn't returned to the system. A larger block of allocated memory on the other hand would in fact be returned. I just wanted to know if maybe it's just Microsoft's compiler or if it is generally a C/C++ thing. (I'm sure the memory is returned after the program terminates though.)

TheHawgMaster - August 27, 2003 02:22 AM (GMT)
What allocation system are you using? GlobalAlloc()? new? malloc()? Maybe VC++ doesn't deem the time involved in de-allocating memory to be worth such a small amount of memory.

mikawo - August 27, 2003 02:00 PM (GMT)
I've tried both new and malloc, but yeah, I'm thinking it's probably VC++. So it doesn't happen at all with your compiler?

dr voodoo - August 27, 2003 05:31 PM (GMT)
QUOTE
I'm thinking it's probably VC++.

THW also has VC++.

You are probably using it wrong. Here a little example with new:
CODE
int*array=new int[82];
delete[]array;
int*var=new int;
delete var;

And the same using GlobalAlloc:
CODE
int*array=(int*)GlobalAlloc(GPTR,sizeof(int)*82);
GlobalFree(array);
int*var=(int*)GlobalAlloc(GPTR,sizeof(int));
GlobalFree(var);

But note that there is a small diffrence between them. new will not fill the memory allocated with 0s but GlobalAlloc (when used with flag GPTR) will. When using new I recomend using ZeroMemory to initialize it.




* Hosted for free by InvisionFree