Common VC memory allocation functions: heapalloc, globalalloc, localalloc, virtualalloc, malloc, and new

Source: Internet
Author: User

 

1. First let's look at heapalloc:
The explanation on msdn is: heapalloc allocates a block of memory from the stack, and the allocated memory is non-movable (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 is to start from the specified address, instead of being allocated from the global heap, unlike gloabalalloc, this may be global or local. Function prototype:
Lpvoid
Heapalloc (
Handle hheap,
DWORD dwflags,
Size_t dwbytes
);
Hheap is the starting position of the Process heap memory.
Dwflags indicates heap memory allocation. Including heap_zero_memory, even if the allocated space is cleared.
Dwbytes is the size of heap memory allocated.
The corresponding space release function is heapfree.
2. Let's look at globalalloc: This function is used to allocate memory from the global heap for the program. The function prototype is:
Hglobal globalalloc (
Uint uflags,
Size_t dwbytes
);
Uflags parameter description
Combination of ghnd gmem_moveable and gmem_zeroinit
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
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. The function is 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 every application or DLL is loaded into the memory, the code segment is loaded into the global heap, and the system allocates a 64kb data segment for each instance from the global heap as the local heap of the instance, used 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.
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. Its function prototype is as follows:
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 submit the pages to be written at will after the memory is retained. 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: malloc and free are standard library functions in C ++/C, which can be used to apply for dynamic memory and release memory. For non-Internal data objects, using malloc/free alone cannot meet the requirements of dynamic objects. The constructor must be automatically executed when the object is created, and the Destructor must be automatically executed before the object is extinct. Since malloc/free is a library function rather than an operator and is not controlled by the compiler, it is impossible to impose the tasks of executing constructor and destructor on malloc/free.
6. New: New/delete is the c ++ operator. It can be used to apply for dynamic memory and release memory. The C ++ language requires a new operator that can complete dynamic memory allocation and initialization, and a delete operator that can clean up and release memory. Note that new/delete is not a database function. C ++ programs often call C functions, while C Programs can only use malloc/free to manage dynamic memory. New is an operator. It has the same status as what "+", "-", "=.
Malloc is a memory allocation function for you to call.
New is a reserved word and does not need to be supported by header files.
Malloc requires the header file library function to support. New to create an object,
Malloc allocates a piece of memory.
You can use a new object as a common object to access it using a member function instead of directly accessing its address space.
Malloc is allocated with a memory area, which can be accessed with a pointer and moved inside.
Memory leakage can be checked out for malloc or new. The difference is that new can indicate the row of the file, and malloc does not have this information. New can be considered as the execution of the malloc and constructor. The new pointer directly carries the type information. Malloc returns the void pointer.
17:17:34

Source: -- http://hi.baidu.com/netspirit/blog/item/c033012c4a26e6e58b139987.html

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.