HeapAlloc or LocalAlloc?

Hello experts!!

I'm very confused: when should i use HeapAlloc and when LocalAlloc?
dbroosAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
They're usually interchangeable. From the docs:

"LocalAlloc
The LocalAlloc function allocates the specified number of bytes from the heap. Win32 memory management does not provide a separate local heap and global heap. "

So, it absolutely makes no difference to write

BYTE* p = LocalAlloc ( LPTR, uBytes);

or

BYTE* p = HeapAlloc ( GetProcessHeap(), HEAP_ZERO_MEMORY, uBytes);

However, if you'd create your own heaps for memory management (using 'HeapCreate()'), the latter one would make sense...
0
 
dbroosAuthor Commented:
Thanks, can you explain me what is the heap?
0
 
dbroosAuthor Commented:
Thanks, can you explain me what is the heap?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
nietodCommented:
if you have to ask what the heap is, then you probably don't want either!

A heap is used for performing dynamic memory allocation.  it allows you to allocate a block of memory at run time,w whose size is defined at run-time and whose life time is not specified by the scope of the code that allocates the memory.  This sort of memory is very different than memory allocated to store non-static local varaibles, whose lifetime is limited to the lifetime of the procedure or scope that defines them. And from global and static varaibles whose lifetime is the entire duration of the program.

However there is rarely a need to use either LocalAlloc or HeapAlloc for dynamic memory allocation.  These are windows specific functions and are only needed on rare occasions when working with the windows OS.   Virtually all of you memory allocation needs can be handled by the C++ new operatior.   Unless you need to do something very windows specific, you should be using new to perform these sorts of allocations.
0
 
jkrCommented:
Thank you for junping in with the explanation nietod (I was just on my way back home from the office, and I don't like to be online when waling the streets :o).
0
 
nietodCommented:
Really?  My modem is surgincally implanted...
0
 
jkrCommented:
>>Really?  My modem is surgincally implanted...

Duh, how backwards are we here...
0
All Courses

From novice to tech pro — start learning today.