6

How does actually malloc get the present free memory space available in the microcontroller. Does it keep a list of areas unallocated continously in the runtime? How does it get information of a previous malloc assignment memory allocation if there are two malloc statements in the code

How can one know which memory is free and which one is not at runtime. At compilation time we can know which all locations in RAM is assigned by the compiler for the variable. Does malloc uses this information to do this.

0xAB1E
  • 721
  • 10
  • 27
  • 2
    It depends. Not all implementations are the same. – David Heffernan Nov 14 '13 at 06:40
  • Try reading this post: http://stackoverflow.com/q/5422061/10077 – Fred Larson Nov 14 '13 at 06:42
  • Iam a novice in this area so please pardon if my question is wrong. Actually in cases where the amount of memory needed may only be known at the run time. So when memory is needed "search for a block large enough to satisfy the request". How can one know which memory is free and which one is not at runtime. At compilation time we can know which all locations in RAM is assigned by the compiler for the variable. Does malloc uses this information to do this. My english is poor if question is not understandable please inform me. – 0xAB1E Nov 14 '13 at 06:52
  • Why do you need to know this? And do you understand that there is no one single answer? – David Heffernan Nov 14 '13 at 07:03
  • Just curious about the implementation that's all. And i just want the algorithm or an idea used to implement that. Don't want to know the whole implementation in detail. Just any one idea a simple one. – 0xAB1E Nov 14 '13 at 07:08
  • 2
    So read the code of a malloc implementation there are plenty around. – David Heffernan Nov 14 '13 at 07:12

1 Answers1

3

As the commentators said above, there are multiple implementations of malloc and the algorithm may vastly vary for each of these implementations. This is a vast and complicated area and you should read up on the memory management to get a complete idea on the topic.

In simple words, all the malloc implementations are backed up by the kernel's memory management schemes. The kernel see the whole system memory as pages for fixed size (4k, 8k etc) and all the allocations and frees are done on the pages. There will be a memory management subsystem exists for all the kernel implementations and which does the accounting of whole memory allocations and frees happening on the system. When you call a malloc, it will eventually reaches this memory management subsystem, and looks for the next available free page from the pool and allocates for the requesting process. Before giving the page to the requester, he will make sure to mark it as used and same way when you free up the memory it will add it back to the free pool and unmark used. There exists so many implementations on how the kernel does all these effectively (read up on memory manager implementations in linux)

In common implementations, there exists a minimal memory manager functionality in the userspace itself. The user space process itself maintains a free pool and when a malloc requests memory, before breaking in to kernel, it will look in its own free pool if memory is available. If available it will mark it up and satisfies the request without the help of kernel. Similarly, when you free up the memory, the freed up chunk of memory will not immediately go back to kernel's free pool instead it will stay with the process's free pool so that next malloc can use this.

As I said in the beginning, this is a huge and complicated topic and you can find a lot of documentations available in the internet about this.

joe
  • 1,136
  • 9
  • 17