Solved

HeapAlloc or LocalAlloc?

Posted on 2001-08-03
7
497 Views
Last Modified: 2011-10-03
Hello experts!!

I'm very confused: when should i use HeapAlloc and when LocalAlloc?
0
Comment
Question by:dbroos
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 6350086
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
 

Author Comment

by:dbroos
ID: 6350200
Thanks, can you explain me what is the heap?
0
 

Author Comment

by:dbroos
ID: 6350206
Thanks, can you explain me what is the heap?
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 22

Expert Comment

by:nietod
ID: 6350271
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
 
LVL 86

Expert Comment

by:jkr
ID: 6350287
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
 
LVL 22

Expert Comment

by:nietod
ID: 6350502
Really?  My modem is surgincally implanted...
0
 
LVL 86

Expert Comment

by:jkr
ID: 6350701
>>Really?  My modem is surgincally implanted...

Duh, how backwards are we here...
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 120
java inner class, for the sole use of parameter passing 21 77
c++ using a LPCOLESTR * and adding a string varaible to the Pointer 2 58
max float value 3 41
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question