how to write own version of malloc
hello friends,
could you guide me how i can write my own version of malloc and free function
Related Article: Malloc: issues in VS2005 but not GNU?
is a solved C discussion thread by dancks that has 3 replies, was last updated 6 months ago and has been tagged with the keywords: c, gcc, malloc, visual, studio, 2005, msvc, 8, mingw.
johnray31
Junior Poster in Training
70 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Read K & R. You'll know how to do that.
By the way why do you want to make your own malloc function?
what is k & r am new in this language and ... i face this question in my interview
johnray31
Junior Poster in Training
70 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
K&R It's a book by the creaters of the C programming language
Ancient Dragon
Achieved Level 70
32,127 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69
> am new in this language and ... i face this question in my interview
If knowing C is a requirement for whatever you're applying for, then you're not qualified, even if you manage to spoof the answer to this question.
You might be able to regurgitate the answer, but you still won't be able to explain it.
If the interviewer asks you what "this" does, will your answer begin with "ummmm"?
Say for argument you get this job (which you're unqualified for), are you going to be back asking people to solve your assignments as well?
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,875
Solved Threads: 953
Skill Endorsements: 27
writing your own malloc is not all that easy as you can see from the GNU implementation. Its largly os-dependent and is probably not something an entry-level programmer could accomplish. And nobody could write it in just the few minutes that an interview could accommodate.:eek:
Ancient Dragon
Achieved Level 70
32,127 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69
writing your own malloc is not all that easy as you can see from the GNU implementation. Its largly os-dependent and is probably not something an entry-level programmer could accomplish. And nobody could write it in just the few minutes that an interview could accommodate.:eek:
Well if that's what they're expecting I'd say -
what a freakin' glorified waste of time!
:rolleyes:
iamthwee
Posting Genius
6,254 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 476
Skill Endorsements: 34
Reciting the simple one from K&R (in some form or other) is pretty doable in an interview context, and shows the interviewer that you know some useful bits of the language.
Writing a good malloc which has decent performance and resists fragmentation on the other hand is a much harder task.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,875
Solved Threads: 953
Skill Endorsements: 27
hey all of you gus " am is new in this language " by this i mean that i have no professional experiance in c... and i was also thinking that it is not easy to write malloc function that is asked by you in just 20 minute ..
and i know well the basics of c programming language and am not giving any assinment to you guys i was just waiting for ur comment to check that is any simpler way to do this.
i tell those peple the whole idea how malloc work and in very simplified manner write a c code to show them so especially MR. SALEM don't guess anything i am a worthy person and quite efficient to do my work . ok
johnray31
Junior Poster in Training
70 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
and my question was like that
they give me an array of memory say 10,000 character array and they ask to write ur own version of free and malloc function to allocate and dealocate the memory from this memory pool?
johnray31
Junior Poster in Training
70 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Actually, this is very easily done...that is if you did some basic C-language based I guess- programming including pointers. In C, since you can use pointer arithmatic on a pointer that you have malloced initially. So, if you are to say make a pooled memory-or given a memory space in which you are allowed to access- do arithmatic pointer and divide up the memory. Use strucs and/or headers to make things simpler. If you are given an "initialized" memory space as a form of an array, than it is easier. Array indexes also satisfies the arithmatic, so you can put -1 or other magic number in an array to indicate its empty, and work your way out from there on.
It's easier than that if the system allocation options are similar enough to malloc() to facilitate a thin wrapper. For example, I wrote an implementation of the standard library that defines malloc as such:
/*
@description:
Allocates space for an object whose size is specified by size.
*/
void *malloc(size_t size)
{
uintptr_t mem = (uintptr_t)_sys_heapalloc(__sys_heap, size + sizeof(size_t));
*(size_t*)mem = 0; /* General alignment */
mem += sizeof(size_t);
return (void*)mem;
}
Where _sys_heapalloc() is nothing more than a portability wrapper around the Win32 API's HeapAlloc():
/*
@description:
Allocate the specified number of bytes from the specified heap.
*/
void *_sys_heapalloc(_sys_handle_t heap, unsigned bytes)
{
return HeapAlloc(heap, 0, bytes);
}
Barring initialization and teardown of the __sys_heap object in the C runtime code, that's all there is to it. And even then it's more complicated than the bare minimum because my library supports C11's aligned_alloc(), hence the bookkeeping information in front of the returned pointer.
deceptikon
Challenge Accepted
3,445 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57
The restriction is you can't use malloc() or anything like it, such as HeapAlloc() and AFAIK that is MS-Windows-specific function.
Ancient Dragon
Achieved Level 70
32,127 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69
The restriction is you can't use malloc() or anything like it, such as HeapAlloc() and AFAIK that is MS-Windows-specific function.
I didn't see any such restriction mentioned in the thread, barring the implied restriction from K&R (though it still uses POSIX sbrk()). My implementation is inherently tied to Windows, and targets the CL compiler specifically for a number of reasons. Could you elaborate on what you meant by the quoted statement?
deceptikon
Challenge Accepted
3,445 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57
I didn't see any such restriction mentioned in the thread,
Since we don't have post numbers anymore I can't just give you a link to it but the op was given a large char array and asked to write his own malloc() and free() functions. I miss those post numbers alot because they were really handy in this kind of situations.
Could you elaborate on what you meant by the quoted statement?
Huh? What quoted statement?
Ancient Dragon
Achieved Level 70
32,127 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69
Since we don't have post numbers anymore I can't just give you a link to it but the op was given a large char array and asked to write his own malloc() and free() functions.
Ah, I see now. It's this post. The time posted for each post is actually a link to the post's permanent URL. You can copy that and link to it instead of referring to post numbers.
Huh? What quoted statement?
Your statement that I quoted in my post with the forum's quote feature. :rolleyes:
deceptikon
Challenge Accepted
3,445 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57
The time posted for each post is actually a link to the post's permanent URL
Maybe you need to make that feature more obvious.
I still don't understand what you want me to elaborate on. I thought what I posted was pretty clear.
Ancient Dragon
Achieved Level 70
32,127 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,575
Skill Endorsements: 69