Hey,
I want to know how to go about creating functions like malloc and free in C(without using any other libraries)? I need to make my own set of memory management functions.
Thanks...
Hey,
I want to know how to go about creating functions like malloc and free in C(without using any other libraries)? I need to make my own set of memory management functions.
Thanks...
I tried this a few days ago.
What I did was to get a chunk of memory from the os (that would require other functions), and give the new function..say memalloc the liberty to allocate from that region.
Is that what you wanted to do?
If not, (for the first part), I don't know of any portable way of doing so.
See A Memory Allocator for an example.
And it's easy to replace the functions provided by compiler or OS libraries - just link in an object file with the correct function definition that you've written and it will be used instead of the default ones. Not that I'd recommend it except for educational purposes.
The moment you step away from malloc, you're in the realm of needing to use OS specific options, so you might want to specify your OS.
GlobalAlloc, HeapAlloc, and VirtualAlloc are other options on Windows.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Of course it is better and safer to use malloc and free, I just made it for a (sort of) virtual machine I was trying to make.
It wasn't actually a new memory manager.
I still used malloc to get the large chunk from the system.
After that I made a huge char array from it, and gave the sizeof(required) amount from it to the required object by casting it to a void*.
I read that this process(except some small details) was an old tradition in unix programming before the advent of the standards.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Of course guestcheap needs to do that.Originally Posted by iMalc
Originally Posted by Ken Thompson
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Unfortunately, it's also a fact of life that most people can't trust code that they did totally create themselves.
hey as far of my knowledge,if you want to create such fnctns then it's better to read few in functions.......
That was actually my goal; to provide an abstraction for allocating memory to objects from the memory owned by the program(there, the virtual machine).
Is that possible with malloc?
First I tried it with realloc but couldn't get the address of the remaining pool(if not returned to the system) after it was done.
I thought that is what 'guestcheap' had in mind; since the other alternative wasn't very viable.
Thanks for the replies...
I got some help from Writing a memory manager - OSDev Wiki. I want to this but I have absolutely no idea how. Any help?
PS:
You know, some people actually 'need' to do things. Of course, I don't personally know any of them.I'd just like to point out that that statement is false. You never 'need' to do such a thing. You might 'want' to do such a thing, which is your own foolish choice, but you never 'need' to do such a thing.