Question

i am student and want to know more about the dynamics memory management. For C++, calling operator new() can allocate a memory block under the Heap(Free Store ). In fact, I have not a full picture how to achieve it.

There are a few questions: 1) What is the mechanism that the OS can allocate a memory block?? As I know, there are some basic memory allocation schemes like first-fit, best-fit and worst-fit. Does OS use one of them to allocate memory dynamically under the heap?

2) For different platform like Android, IOS, Window and so on, are they used different memory allocation algorithms to allocate a memory block?

3) For C++, when i call operator new() or malloc(), Does the memory allocator allocate a memory block randomly in the heap?

Hope anyone can help me.

Thanks

Was it helpful?

Solution

malloc is not a system call, it is library (libc) routine which goes through some of its internal structures to give you address of a free piece of memory of the required size. It only does a system call if the process' data segment (i.e. virtual memory it can use) is not "big enough" according to the logic of malloc in question. (On Linux, the system call to enlarge data segment is brk)

Simply said, malloc provides fine-grained memory management, while OS manages coarser, big chunks of memory made available to that process.

Not only different platforms, but also different libraries use different malloc; some programs (e.g. python) use its internal allocator instead as they know its own usage patterns and can increase performance that way.

There is a longthy article about malloc at wikipedia.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top