|
-
Mar 6th, 2002, 12:01 PM
#1
Thread Starter
PowerPoster
Differents...
Can someone explain about the different between each of the following item, also which is the best to use?
LocalAlloc
GlobalAlloc
VirtualAlloc
HeapAlloc
char *lpszBuffer = new char[128];
thanks
-
Mar 6th, 2002, 02:08 PM
#2
LocalAlloc and GlobalAlloc had only special meanings in 16-bit windows. They are deprecated and shouldn't be used anymore.
HeapAlloc allocates memory on a heap of the process. You can get the process' default heap with the GetProcessHeap function. This function is ideal for allocating small blocks of memory (up to 512 kB).
VirtualAlloc is more complicated. It can reserve and commit virtual memory, which means that the memory is more likely to get swapped to the swap file. It does that only in large blocks (the page size, usuallly 512 or 1024 kB). Use it for allocating large amounts of memory.
All these functions are WinAPI and therefore only usable when programming for windows.
malloc is the standard memory allocation function of the C runtime. It is guaranteed to work on every computer. In windows-based runtime libraries it results in a call to HeapAlloc.
new is a new keyword of C++. Like malloc it is guaranteed to be cross-platform compatible. Internally calls to new usually result in calls to malloc (and therefore to HeapAlloc on win systems), but for one you must not trust that internals stay the same (and you can override the implementation of new) and also new calls the constructor of classes (no other method does this).
When you create windows apps, use HeapAlloc for small blocks, VirtualAlloc for large blocks of memory and new for class objects and such.
When you create cross-platform apps, use new for everything.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|