Although we usually writeProgramIt applies for memory using the new or malloc of the c Runtime Library. In fact, the C Runtime Library also calls a set of Apis provided by windows to manage heap memory. Including: heapcreate, heapdestroy, heapalloc, heapfree...
To use heap memory, each process needs to call heapcreate to create a private heap object for itself.
Handle winapi heapcreate (DWORD floptions, size_t dwinitialsize, size_t dwmaximumsize)
Floptions: heap option. 0 is input to the C Runtime database. The default value is used.
Dwinitialsize: the initial size. The value is 4096 in the C Runtime Library.
Dwmaximumsize: maximum value that can be allocated. input 0 to the C Runtime database, indicating that a large memory size can be applied.
The function returns a handle, which indicates the heap object, which is required by other APIs.
Before the process is destroyed, you should call heapdestroy to destroy this object. This function is very simple. Just pass in the handle.
After applying for heapalloc, you can call heapalloc to apply for heap memory.
Lpvoid winapi heapalloc (handle hheap, dwflags, size_t dwbytes)
Hheap: Return Value of heapcreate.
Dwflags: input 0 to the C Runtime database. The default value is used.
Dwbytes: the actual size of heap memory to be applied.
The address of the memory returned by the function.
Call heapfree.
Bool winapi heapfree (handle hheap, DWORD dwflags, lpvoid lpmem)
Hheap: the return value of heapcreate, which can also be obtained by calling getprocessheap.
Dwflags: Continue to 0.
Lpmem: The heapalloc return value and heap memory address.
These APIs are encapsulated in the C Runtime Library and some features are added.