Re: Implementing malloc...
I can't recall the exact details. Please correct me if I am wrong. It is roughly something like, for each allocated memory block, the first few bytes (header) have to store the memory block size. Beside that, this header also store a pointer to the next allocated memory block. In other words, the headers form a linked list that you can trace from one allocated memory block to another. Similarly, to mark where are your free memory blocks, free memory blocks also reserve a header with pointer to the next free memory block as well as the size of the present memory block.
Re: Implementing malloc...
Quote:
Originally Posted by Kheun
I can't recall the exact details. Please correct me if I am wrong. It is roughly something like, for each allocated memory block, the first few bytes (header) have to store the memory block size. Beside that, this header also store a pointer to the next allocated memory block. In other words, the headers form a linked list that you can trace from one allocated memory block to another. Similarly, to mark where are your free memory blocks, free memory blocks also reserve a header with pointer to the next free memory block as well as the size of the present memory block.
I'm aware of the fact that a linked list will need to be used (that comes later in the project.) I just don't know how to begin writing the method, do I need to make some special system call or something? Kind confused right now :rolleyes: . I'll look up some stuff online and see what I can get.
Re: Implementing malloc...
Quote:
Originally Posted by YourSurrogateGod
do I need to make some special system call or something? Kind confused right now
If you want to use virtual memory for your customize heap, you need to use system calls. For example, in Windows, you can use VirtualAlloc(), VirtualFree(), etc. If not, the easiest is to use a block of global memory, in which you may have to increase the stack size.