7

For an interview question: how would I write new "malloc" and "free" functions? I don't think "using new and delete" would be an acceptable answer or using something similar like LocalAlloc/HeapAlloc

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
Johnny Pauling
  • 12,701
  • 18
  • 65
  • 108
  • Yes, Windows, sorry for not putting it into the question – Johnny Pauling Feb 06 '13 at 08:51
  • Were you given any API you can use? – StoryTeller - Unslander Monica Feb 06 '13 at 08:53
  • Was the question about creating your own memory manager or about the way Windows' memory manager works? – Alex Feb 06 '13 at 08:53
  • 6
    Memory management is usually taken care of by OS, so you at least should make it more clear, if a new memory manager is needed or you only need functions using some existing API. – KBart Feb 06 '13 at 08:54
  • 1
    Will this help? http://login2win.blogspot.in/2011/06/c-heaps.html – NeonGlow Feb 06 '13 at 08:56
  • 1
    I think this post on SO might answer your question, [How malloc is implemented?][1] [1]: http://stackoverflow.com/questions/3479330/how-is-malloc-implemented-internally – Raj Feb 06 '13 at 08:58
  • 1
    I would start by looking closely at *why* I was doing so. What was wrong with the existing allocator? What were my requirements? – David Schwartz Feb 06 '13 at 09:02
  • @DavidSchwartz, agreed entirely, you have to find a good reason and be sure you can improve on what's there. My experience is that in many contexts you can do better than the default offering. See an old question of mine here for an example; http://stackoverflow.com/questions/2469738/unusual-heap-size-limitations-in-vs2003-c Running multiple heaps for different sized objects, and pooling similar objects that you have lots of, can provide massive performance gains. Similalrly, many of the standard containers don't scale well to huge data, it's all about context. – SmacL Feb 06 '13 at 09:12
  • 1
    @ShaneMacLaughlin: Right. You might need predictable behavior across different platforms. You might need better concurrency in a multi-threaded case. You might need better tracking of where memory was allocated. You might need to allocate a bunch of memory and free it as a group. You might need to be more efficient at returning free memory to the OS. You might need better run-time statistics of memory use. To ask how you would do it without knowing why is crazy. (And perhaps, that's the answer they're looking for.) – David Schwartz Feb 06 '13 at 09:15

7 Answers7

12

Since this is for an interview question, it probably won't have a "right" answer, and they're more interested in your thought process, and your general knowledge surrounding the topic.

You should ask to clarify the requirements, or give a range of answers depending on the situation. Here are some issues to consider:

  • Is it important to avoid memory fragmentation? A fixed alloc size may help.
  • Do you need speed guarantees for allocation? Use index of used/free blocks.
  • Do you need to easily trace the allocations performed? Add logging structures to the malloc/free library.
  • Do you need to protect against or detect memory under/overruns? Extra padding and periodic padding checks.
congusbongus
  • 13,359
  • 7
  • 71
  • 99
  • 3
    +1 It's not an answer they're looking for. It's how you talk to them about the question and whether you do or don't demonstrate knowledge of the subject area. – David Schwartz Feb 06 '13 at 09:16
4

Simply put,

Your malloc function should be able to get memory from the operation system and give it to the client(say, your C program) that requests for memory to be allocated dynamically. To implement this, the malloc library usually has many data structures (depending on implementation) - For eg, the malloc implementation may choose to keep track of free blocks of memory using a linked-list. A more efficient way would be to have a list of linked lists, each containing blocks of a paticular size range.

I happened to work on TCMalloc library, that might help you understand this more clearly

Memory Allocation through free-lists

Memory allocation through freelists

Lets say, class 0 is a linked list of free blocks of size 4k, class 1 is a linked list of free blocks of size 8k and so on.

When a call to malloc is made (say, of size 10k), your malloc implementation traverses through the freelist to find out the smallest free block that would satisfy the request (In this case, a 4k block would not be sufficient, so it'll fetch an 8k block, remove it from the freelist, and return it to your program).

Similarly, when a call to free is made, your implementation(among other things) should reclaim the block from the program and adds it in the appropriate place to one of the freelists.

Aadishri
  • 1,341
  • 2
  • 18
  • 26
Raj
  • 4,342
  • 9
  • 40
  • 45
3

This sites answers your question.

http://www.codeproject.com/Questions/177316/Write-your-own-malloc -- same question http://www.inf.udec.cl/~leo/Malloc_tutorial.pdf

napat
  • 51
  • 1
  • 5
3

If an interviewer ask you

how would I write new "malloc" and "free" functions?

And you start talking about what you are going to do, you have already failed. Before hand you need ask few questions to have a more precise usage of your malloc function. On top of my head some of the first questions are:

  • For which OS?
  • For which memory? User malloc or Kernel malloc?
  • Which is the most frequent usage scenario? All purpose malloc ? Better for huge chunks or small chunks?

There need to be a first round of requirement elicitation before even starting talking about code.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
  • 3
    "you have already failed" - that's a rather grim way of looking at things. – Neowizard Feb 06 '13 at 09:10
  • 1
    @Neowizard believe me. you **do**. There will be at least one candidate which will follow what Cong and me are suggesting. And in an interview, this candidate will be already ahead of you. – UmNyobe Feb 06 '13 at 09:13
  • 1
    I've never been to an interview where the interviewer asked me "trick" questions. Giving only a portion of the relevant question information isn't a valid way (IMO) to test the candidate's abilities or way of thinking...but that's just me. – Neowizard Feb 07 '13 at 06:20
  • 1
    it's not a trick question. If somebody tell you "build me a bridge", and you go to work without even knowing where is the bridge going to be, your work is going to be pointless. – UmNyobe Feb 07 '13 at 08:41
  • 1
    If somebody tells you to build a bridge during an interview, it's going to be a very long interview. If somebody asks me to write something at work it's a completely different situation than asking me to "write" something in an interview. An interviewer who doesn't understand that difference is another problem all together. – Neowizard Feb 09 '13 at 05:34
1

It depends on the requirements, for instance say you want to create embedded software where it sometimes it is not allowed to do malloc/free during runtime or because of performance reasons. You could then instead create your own version of malloc/free that do not allocate/free memory but instead just takes/retruns blocks from a preallocated heap.

AndersK
  • 35,813
  • 6
  • 60
  • 86
1

If you are talking about hooking malloc() and free() calls, you can do that with something like this:

#define malloc(x) your_malloc(x)
#define free(x) your_free(x)

But, if you're talking of creating your own memory allocator, then you might want to look at one of the implementations of heap allocator, by Kernighan and Ritchie in their book "The C Programming Language 2nd Edition". It basically relies on using sbrk() system function to increase the size of the data segment for the process.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

I guess they could/should word the question another way: "How is heap memory managed by malloc and free?".

Then you can think of the problem in a slightly different way - given a linear list of values how do you partition out chunks of the list and keep track of them, how do you give chunks of that list back, how do you manage fragmentation of the list etc.

In short though the address of the next free block is stored in heap next to the block that's allocated out, the information for where the next free piece of memory is stored is invisible to the user but is still part of the same heap. This is why overrunning mallocated memory can completely corrupt the heap and cause the likes of malloc and free to crash.


Usually the interviewer is using the question to give you free reign to demonstrate your knowledge. You can start with basic functionality and then if you know your onions go on to how different O/Ss manage it, what the different algorithms are etc. After all, whatever you would describe is unlikely to encompass the last twenty years of R&D on the subject!

Joe
  • 7,378
  • 4
  • 37
  • 54