I'm new to Linux and C, just want to ask how brk pointer grow after calling malloc. We know that the kernel maintains a brk pointer that points to the top of the heap. let's say there is no available free blocks when we call malloc to allocate the requested memory and we know malloc call mmap internally. Below is my questions:

Q1-when malloc call mmap internally, does malloc pass brk as the first argument to mmap as starting address like:

//not 100% correct onlyfor demo purpose
// void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);

void *malloc(size_t size){
  mmap(brk, size,...);  
  ...                    //line x

Q2- does other code section(after line x above) inside malloc make brk grow? or mmap internally make the brk grow?

  • The implementation is much more complicated. You can look at the source code of malloc. See stackoverflow.com/a/6485675/10622916.
    – Bodo
    Sep 23, 2020 at 12:46
  • "we know malloc call mmap internally" - Not always malloc existed before the mmap call existed.
    – icarus
    Sep 23, 2020 at 13:00

1 Answer 1


The specifics depend on the malloc implementation, but at least in Doug Lea’s implementation (as used by default in the GNU C library), mmap and sbrk are used independently.

  1. When malloc calls mmap, it uses mmap with no base address; effectively, it delegates the allocation to the kernel (after adjusting the requested size to take into account alignment constraints). There’s no expectation that the mmaped memory is contiguous with the existing heap, or that it can be re-used for any other allocation.

  2. mmap doesn’t touch brk. If malloc doesn’t end up using mmap, it calls sbrk directly. The implementation is described in detail in comments at the top of the file; if you want to follow the malloc code, look for the sysmalloc function, and bear in mind that mmap is MMAP and sbrk is MORECORE in this code.

  • Thank you for your answer. Still a little bit confused. 1-if malloc call mmap without base address, how does the kernel know that it should allocate new virtual memory in the heap area? it might allocate it in other areas which is not the heap 2-what happen if malloc uses mmap, then how brk get updated?if mmap doesn’t touch brk that means malloc will update brk?
    – slowjams
    Sep 23, 2020 at 13:44
  • 1
    1. When malloc uses mmap, it doesn’t want to allocate memory on the heap. 2. When malloc uses mmap, the program break doesn’t change. Sep 23, 2020 at 14:03
  • malloc requests heap memory, that is the purpose we use malloc, isn't it? if malloc uses mmap which doesn't allocate memory on the heap, then how do we get heap memory?
    – slowjams
    Sep 23, 2020 at 22:49
  • I’ve answered your SO question re. the above. Sep 24, 2020 at 5:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .