I have an exam coming up where we have to study heaps and stacks. The stacks part I understand, but the heap is giving me trouble.
Our book gives us both in C and in Assembly. But I am having difficulty understanding both of these concepts.
I will just put up a screenshot of the book so it looks nicer.
Questions:
-
How does Heap_Init work?
-
How does Heap_Allocate work?
-
How does Heap_Release work?


Thank you all for your time and effort.
The heap in this sense of the word is an area of memory not on the stack which the program can use to store larger amounts of data.
However, during the lifetime of the program, that program must track its own memory usage. Specifically, the program cannot write to its entire virtual address space on a whim; if the address to which it tries to write is not mapped by the operating system that will trigger an access violation, which will result in your program receiving a segmentation fault and being terminated.
So, the program being run must ask the operating system to allocate some space. If it can, the operating system will do this, and will map that space into the virtual address space.
However, if you need to release a portion of the memory, you need to know where it is and what you are doing with it. Also, you need to know, as the program given a particular size of memory where to store it. See, things get complicated very quickly – if you’re doing a lot of memory allocating from say C, and then frequently freeing again, it doesn’t make sense to have a 1-1 map between allocations and frees and system calls to the same effect. Instead, it would be better if the memory allocator just pulled a bigish chunk of memory from the operating system and then kept track of that.
Enter the need for a storage structure to track what of the allocated space is currently in use, and what ranges in the virtual address space are marked out for use.
So,
That’s the theory of what these functions will do. If you look at the code, you should be able to work out what your specific implementation is doing.