Monday, June 02, 2008

Memory allocation in C++

All I had to do was read some content from Registry, but it was not all that simple, I had to read 1 registry, then use the content to read another string and then use it.

While doing all this, I didn't want to use static size buffers, so I started doing malloc(), but guess what malloc just refused to allocate 104 TCHARs, hmm then I tried HeapAlloc() and that was able to allocate the memory.

Different ways to allocate/de-allocate memory:

malloc HeapAlloc HeapCreate
calloc GlobalAlloc GetProcessHeap
realloc LocalAlloc HeapFree
new   HeapDestroy
    VirtualAlloc

small amount of memory should be allocated through malloc family. For bigger size user HeapAlloc, GlobalAlloc and LocalAlloc are available only for backward compatibility, so should not be used.

For more detail information on Managing Heap Memory in Win32, refer : http://msdn.microsoft.com/en-us/library/ms810603.aspx

No comments: