Similarities and differences between memory management functions heapalloc, localalloc, virtualalloc, malloc, and new

Source: Internet
Author: User

I am looking for a job recently. It's so annoying !!!!!!!!

Forget it. Instead of getting depressed, take notes first and change your mind (or learn something interesting !).

Refer to blog:

Http://blog.csdn.net/fgh_555/article/details/2560774

Http://hi.baidu.com/gamedot/item/0727f36eb4fa65116995e6d8


1. heapalloc: heapalloc allocates a block of memory from the heap, and the allocated memory cannot be moved (that is, if no contiguous space can meet the allocated size, the program cannot use other scattered spaces, leading to allocation failure ). The allocation method starts from the specified address, but unlike gloabalalloc, it is allocated from the global stack, which may be global or local.

Function prototype:
Lpvoid
Heapalloc (
Handle hheap,
DWORD dwflags,
Size_t dwbytes
);

Parameter introduction:
Hheap is the starting position of the Process heap memory.
Dwflags indicates heap memory allocation.

Heap_zero_memory is cleared even if the allocated space is cleared.
Dwbytes is the size of heap memory allocated.
The corresponding space release function is heapfree.


2. globalalloc: This function is used to allocate memory from the global heap for the program.

Function prototype:
Hglobal globalalloc (
Uint uflags,
Size_t dwbytes
);

Parameter introduction:
Uflags parameter description
Gmem_fixed allocates fixed memory. The returned 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 returned value is the handle of the memory object. The globallock function can convert the handle to a pointer.
Gmem_zeroinit initializes the memory content to zero
Gptr gmem_fixed and gmem_zeroinit combination

Combination of ghnd gmem_moveable and gmem_zeroinit
Generally, during programming, the memory allocated to the application can be moved or discarded, so that the limited memory resources can be fully utilized, the address of the memory we allocated at a certain time is uncertain, because it can be moved, so we must first lock the memory block, here, the application needs to call the globallock function of the API function to lock the handle. Lpmem = globallock (hmem); in this way, the application can access this memory. Therefore, we usually use globallock when using globalallock. Of course, we must remember to use globalunlock when we do not use memory. Otherwise, the locked memory block cannot be used by other variables.
The function for releasing space corresponding to globalalloc is globalfree.


3. localalloc: This function is used to allocate memory from the local heap for the program.

Function prototype:
Hlocal localalloc (
Uint uflags,
Size_t ubytes
);
The parameter is the same as globalalloc.
There is a difference in 16-bit windows, because in 16-bit windows, a global heap and a local heap are used to manage the memory. When each application DLL is loaded into the memory, the code segment is loaded into the global heap, the system allocates a 64kb data segment from the global heap for each instance as the local heap of the instance to store the application stack and all global or static variables. Localalloc and globalalloc are used to allocate memory in the local or global heap respectively.
Because the local heap of each process is small, the allocation of memory in the local heap is limited by space. However, this heap is private to every process, and data allocation is relatively safe. Data Access errors do not affect the entire system.
The memory allocated in the global heap is shared by various processes. Each process can access this memory as long as it has a handle of this memory block, however, each global memory space requires additional memory overhead, resulting in a waste of allocation. In addition, serious errors may affect the stability of the entire system.
However, in Win32, each process has only one private heap that is missing from the province. It can only be accessed by the current process. Nor can applications directly access the system memory. Therefore, in Win32, both the global heap and local heap point to the process's provincial heap. There is no difference in allocating memory with localalloc/globalalloc. Even the memory allocated by localalloc can be released by globalfree. Therefore, you do not need to pay attention to the difference between local and global programming in Win32. The general memory allocation is equivalent to heapalloc (getprocessheap (),...).
The release function corresponding to localalloc is lockfree.

Globalalloc and localalloc are both memory allocation functions provided by Windows systems. Their differences are derived from the 16-bit code era. At that time, there was no memory space for different processes, globalalloc is allocated on the global and public remote stacks, while localalloc is allocated on the near-heap of the task itself. On the Win32 platform, these two functions are identical and are all allocated in the memory space of the process itself. After the lock, the result is a normal pointer (32-bit near pointer ).


4. virtualalloc: This function is used to reserve or submit some pages in the virtual address space of the calling process. If mem_reset is used for memory allocation and the allocation type is not specified, the system automatically sets it to 0.

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 submit part or all of a region by calling multiple times in parallel to reserve a large memory area. Submitting the same region for multiple calls will not cause failure. This allows an application to retain the memory and submit the pages to be written at will. When this method is not effective, it releases the application and checks the status of the page to see if it has been submitted before the call is submitted.
The release function corresponding to virtualalloc is virtualfree.


5. malloc: Generally, the implementation of malloc is not allocated from the system heap, but allocated from the heap managed by the Runtime Library connected by the compiler. In the compilation results of development tools on the Win32 platform, heapcreate is usually used to create a heapcreate stack, heapalloc and heaprealloc are used to maintain the heap space growth, and heapdestroy is used to delete the heap at last. When malloc is used for allocation and free release, the code of the Runtime Library allocates space from the heap and returns the space to the heap, and maintains the data structure in the heap.

Because the management of the malloc heap is managed by the Runtime Library, when we use the static Runtime library, if malloc is used to allocate memory in one DLL and free is used to release it in another DLL, the problem usually occurs because each DLL is connected to the code of the Runtime Library, thus there is also a local heap of its own, and when free is used to release it, it will assume that this memory is allocated in its own heap, leading to errors. The memory allocated through globalalloc and localalloc does not exist.

Heapcreate completes heap creation. heapalloc, heaprealloc, and heapfree are all functions for allocating and releasing memory from the heap. That is to say, the Windows system has provided us with a complete set of operations to use our own local heap, but we do not see the method for specifying the allocation policy. According to the source code provided by the compiler, the main functions of the malloc, realloc, and free functions in VC are implemented using these API functions, while the implementation in BC is quite complicated, it seems that a set of its own collections is maintained. It is said that the memory-to-memory ratio of BC is faster than that of VC, which is probably the reason.

The release function corresponding to malloc is free.

 

6. New: The C ++ operator, mainly used to create classes. It is related to the constructor and exception mechanism of c ++, and is consistent with the environment used by other functions above. In general, the new in the compiler uses malloc to allocate memory.

The release function corresponding to new is Delete.


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.