Hi guys, I have buffer with VERY Big SIZE , how could I implement malloc and free functions without using with already functions malloc and free that C provides, any help how can I do that?
Appreciated.
Hi guys, I have buffer with VERY Big SIZE , how could I implement malloc and free functions without using with already functions malloc and free that C provides, any help how can I do that?
Appreciated.
Which OS and Compiler ?
What constitutes "VERY Big SIZE" ?
10's of gigabytes?
How much physical memory do you really have?
Because just reimplementing malloc "for fun" won't get you past various system restrictions.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
The basic idea is to store extra information before each object's storage in the buffer. Storing the size of the allocated block is the minimum amount of information. In practice, it's better to store pointers, too, which connect that block to a doubly-linked list of blocks.
You need to keep track of the unused blocks as well, including the "holes" left from freeing blocks. So you would have two linked lists, used and unused.
When allocating memory you search the unused list for a block that's large enough, move it from the unused to used list, and serve it up. If no block is large enough, return NULL.
When freeing memory, you need to move the block from the used to unused list and coalesce it with any contiguous blocks.
This is all ignoring alignment isses, the upshot of which is that the served-up data blocks need to be properly aligned for the data they will hold. For something as general as malloc, the returned blocks are aligned for the largest possible basic type, I think usually 8 or 16 bytes.
Ordinary language is totally unsuited for expressing what physics really asserts.
Only mathematics can say as little as the physicist means to say. - Bertrand Russell
It's as complicated as you want to make it.
You could write something completely dumb in 20 lines.
You could write something smarter in 100 lines (see the example in K&R-2)
You could do something very sophisticated in 500 lines.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
could you attach me the link for the example, I just entered here K&R2 solutions:Chapter 2:Exercise 8 - clc-wiki and didn't find malloc/free implementation, thanks alot.
So now you're too lazy to even look through those exercises to find it?
Ordinary language is totally unsuited for expressing what physics really asserts.
Only mathematics can say as little as the physicist means to say. - Bertrand Russell
Read Chapter 8
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson