The algorithm I use in my C library is a type of buddy allocator (although I haven't implemented it fully, because it does not merge free blocks): the allocator maintains both a binary tree of all blocks and an array of linked lists of free blocks, sorted by base 2 logarithm of size. Allocation comes from the linked lists until they are emptied, at which point larger blocks are split. Freeing goes exclusively into the linked lists, although with merging of free blocks, this would be more complicated. The tree is used to find the size of allocated blocks, and tree nodes are allocated by a separate fixed-size allocator private to the heap.
The relevant code is pretty short:
https://github.com/nickbjohnson4224/flu ... b/malloc.c. Obviously, don't just copy this code, but feel free to use it to understand the mechanism in more detail.
You may also want to look at glibc's dlmalloc; its implementation is probably a lot less buggy than mine.
I know that it grows up toward the stack and the space inbetween the heap and stack is called the break, which
is why the linux system call has the brk routine. But How do you get the beginning of the heap?
brk() returns the base of the block of memory that was allocated by incrementing the break, so you would use that pointer as the beginning of the heap.