I am doing a project on dynamic memory management. I run into a confuse about the HeapCreate and HeapAlloc functions.
For the HeapCreate() function, we can create a heap and the function will return a HANDLE. We can initialize the size of heap.
Let's say winHandle = HeapCreate( 0, 2 * 1024, 0);
Then, I can the HeapAlloc function to allocate on this heap. But I am confuse about the size of the heap. I try an example, I call the HeapAlloc( winHandle, 0, 1024) twice on this heap, so the total will be 2 * 1024. But I can still call the HeapAlloc many times without run into an error.
Let's say I call the HeapAlloc( winHandle, 0, 1024) three times. The total size of allocation will be 3 * 1024. It is larger than the heap size 2 * 1024. But no error.
Can anyone help me to answer this question?
Thanks,
Here is test code.
// create heap, and return a headle(id) for that heap
HANDLE winHandle = HeapCreate( 0, sizeof(Dog), sizeof(Dog) );
// allocate the heap header to that handle
void* s = HeapAlloc( winHandle, 0, sizeof(Dog) );
// check if the alloc is success or not
assert( 0 != s );
printf("%p \n", s);
// load the heap header data
Dog* heapHeader = new(s) Dog( 1, 2, 4);
// allocate the heap header to that handle
void* ss = HeapAlloc( winHandle, 0, sizeof(Dog) );
// check if the alloc is success or not
assert( 0 != ss );
printf("%p \n", ss);
// load the heap header data
Dog* heapHeadder = new(ss) Dog( 1, 2, 4);