Similarities and differences of heapalloc,globalalloc,localalloc,virtualalloc,malloc,new

Source: Internet
Author: User

1. First we look at HeapAlloc:
The explanation on MSDN is that HeapAlloc is allocating a piece of memory from the heap, and the allocated memory is not movable (that is, if there is no contiguous space to accommodate the allocated size, the program cannot exploit the other scattered space, resulting in an allocation failure), which is allocated from a specified address. Unlike Gloabalalloc, which is distributed from the global heap, this could be global or local. The function prototypes are:
LPVoid
HeapAlloc (
HANDLE Hheap,
DWORD DwFlags,
size_t dwbytes
);
Hheap is the start location of the process heap memory.
Dwflags is a flag that allocates heap memory. Including Heap_zero_memory, even if the allocated space is zeroed.
Dwbytes is the size of the allocated heap memory.
Its corresponding free space function is heapfree.
2. Look again at GlobalAlloc: This function is used to allocate memory from the global heap for program use, the function prototype is:
Hglobal GlobalAlloc (
UINT Uflags,
size_t dwbytes
);
Uflags parameter meaning
Combination of ghnd gmem_moveable and Gmem_zeroinit
gmem_fixed allocates fixed memory, the return value is a pointer
Gmem_moveable allocates active memory, in Win32, memory blocks cannot be moved in physical memory, but can be moved in the default heap. The return value is a handle to the memory object, and the handle can be converted to a pointer using the function GlobalLock
Gmem_zeroinit Initialize memory contents to zero
Combination of gptr gmem_fixed and Gmem_zeroinit
In general, when we are programming, the memory allocated to the application is movable or can be discarded, so that the limited memory resources can be fully utilized, so at some point we allocate the memory of the address is indeterminate, because he can be moved, so we have to lock the block of memory first, Here the application needs to call the API function GlobalLock function to lock the handle. as follows: Lpmem=globallock (HMEM); This allows the application to access this memory. So when we use globalallock, we usually use the GlobalLock, of course, when we do not use memory, we must remember to use GlobalUnlock, otherwise the locked memory block can not be used by other variables.
The GlobalAlloc corresponds to a function that frees space for GlobalFree.
3. LocalAlloc: This function is used to allocate memory from the local heap for the program to use, the function prototype is:
Hlocal LocalAlloc (
UINT Uflags,
size_t ubytes
);
Parameters are the same as GlobalAlloc.
There is a difference in 16-bit Windows because 16-bit Windows uses a global heap and a local heap to manage memory, and when each application or DLL is loaded into memory, the code snippet is loaded into the global heap, and the system allocates a 64KB data segment from the global heap for each instance as the local heap of that instance That holds the stack and all global or static variables for the application. The Localalloc/globalalloc is used to allocate memory in the local heap or the global heap, respectively.
Because the local heap of each process is small, allocating memory in the local heap is constrained by space. But this heap is private to each process, relatively safe to allocate data, and data access errors do not affect the entire system.
The memory allocated in the global heap is shared for each process, and each process can access the memory as long as the handle that owns the block of memory is available, but each global memory space requires additional memory overhead, resulting in wasted allocation. And once a serious error occurs, it may affect the stability of the system as a whole.
In Win32, however, each process has only one private heap that is missing, and it can only be accessed by the current process. Applications also cannot directly access system memory. So in Win32, both the global heap and the local heap point to the process's default heap. There is no difference in allocating memory with Localalloc/globalalloc. Even LocalAlloc allocated memory can be freed by GlobalFree. So programming under Win32, without noticing the difference between local and global, the general memory allocation is equivalent to HeapAlloc (GetProcessHeap (),...).
The LocalAlloc corresponding release function is Lockfree.
4. VirtualAlloc: The function's function is to call the process's virtual address space, book or submit a portion of the page, if used for memory allocation, and the allocation type does not specify Mem_reset, the system will be automatically set to 0; its function prototype:
LPVOID VirtualAlloc (
LPVOID lpaddress,//region to reserve or commit
size_t dwsize,//size of Region
DWORD Flallocationtype,//type of allocation
DWORD flprotect//type of Access Protection
);
VirtualAlloc can persist a large area of memory by committing part or all of a region in parallel multiple invocations. Multiple invocations that commit the same area do not cause a failure. This allows an application to leave the memory free to submit pages that will be written. When this method is not valid, it frees the application to see if it has been committed before committing the call by detecting the state of the page being retained.
The VirtualAlloc corresponding release function is VirtualFree.
5. Malloc:malloc and free are standard library functions in the C++/C language that can be used to request dynamic memory and release memory. For objects of non-intrinsic data types, light Malloc/free cannot meet the requirements of dynamic objects. Objects are automatically executed when they are created, and the object executes the destructor automatically before it dies. Because Malloc/free is a library function and not an operator, the task of executing constructors and destructors cannot be imposed on malloc/free, not within the control of the compiler.
6. New:new/delete is an operator of C + +. Can be used to request dynamic memory and free memory. The C + + language requires an operator new that can perform dynamic memory allocation and initialization, with an operator delete that can perform cleanup and release of memory work. Note New/delete is not a library function. C + + programs often have to invoke the function, whereas C programs can only use Malloc/free to manage dynamic memory. New is an operator, and what "+", "-", "=" ... Have the same status.
Malloc is a function that allocates memory for you to invoke.
New is a reserved word and does not require header file support.
malloc requires the support of the header vault function. New is an object that is created
malloc allocates a piece of memory.
New object You can use it as an ordinary object, access it with a member function, and do not directly access its address space.
Malloc allocates a chunk of memory, accesses it with a pointer, and can move the pointer inside.
A memory leak can be checked for malloc or new, except that new can indicate the row of that file, and malloc does not have that information. New can be thought of as the execution of malloc plus constructors. The new pointer is directly with the type information. and malloc returns a void pointer.

Heapalloc,globalalloc,localalloc,virtualalloc,malloc,new similarities and differences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.