2

Since I was introduced to the concept of heap of a process, I have been assuming that the OS allocates it at the creation of the process. But then I was doing some research and read a statement here. It says:

When a program asks malloc for space, malloc asks sbrk to increment the heap size and returns a pointer to the start of the new region on the heap.

If I understood what's been said, the OS allocates 0 cell for the process's heap, and it is only by calling malloc that the process gets some heap cells. And for me this makes more sens for the expression "dynamic allocation". Is this correct ?

Ayoub Omari
  • 806
  • 1
  • 7
  • 24

1 Answers1

1

in figure you can see that your c/c++ program have a free memory area where the heap and the stack can grow until full the region, so Initialy the heap is empty, and when a process call malloc, Normally (but in modern implementation, malloc prefer to call always mmap()) he call the sbrk() function for increase the memory size of the heap (in reality he first search into the free linked list and if there is not any entry into the linked list he call sbrk(), see this for a implementation of malloc() malloc implementation?). So the os don't directly decide how the heap of a process should be allocated, in c/c++ the thinks work like this, but i think that in other languages the thinks can be slightly different.

Initialy the heap is empty

Holeryn
  • 387
  • 1
  • 4
  • 11
  • 1
    This illustration is mostly obsolete for modern systems: instead of `sbrk`, processes use `mmap` to get memory blocks from the system, which may have virtual addresses above the stack. The layout of the heap, stack, command line arguments and environment variables is system specific. – chqrlie Aug 17 '20 at 16:27
  • The illustration was just for clarify the point, but accordingly to the man documentation malloc use always sbrk() for allocating block of memory lesser than MMAP_THESHOLD bytes , else it use mmap – Holeryn Aug 17 '20 at 16:40
  • The [man page](https://man7.org/linux/man-pages/man3/malloc.3.html) note you are referring to starts with `Normally,`... it used to do that with old implementations of `malloc()` but modern tend to use `mmap` for everything. – chqrlie Aug 17 '20 at 16:50
  • mhhh ok i got the point, but i don't understand why, sbrk just modify the value of the program break, instead mmap is more complex than this, why use it for everything? – Holeryn Aug 17 '20 at 16:58
  • For multiple reasons: the amount of memory accessible via `sbrk()` may be limited by the system and it must form a contiguous range of memory addresses, which can be further limited by the addresses where the stack and dynamic libraries got mapped to. Another reason is to randomise the placement of memory blocks, especially on 64-bit systems to make flaws more difficult to exploit. Another reason is `malloc()` may use multiple arenas but use the same code for all, hence `mmap`. – chqrlie Aug 17 '20 at 17:07
  • @Holeryn I am not sure that the same thing applies to stack. For me The stack is preallocated, and when we say that its size is increasing/decreasing it is the program which increments its stack pointer and decrements it (when calling/returning from a method) and not the OS. – Ayoub Omari Aug 17 '20 at 17:11
  • @chqrlie i have understand Thanks for your comments – Holeryn Aug 17 '20 at 17:13
  • @Quade i think that the stack was preallocated in old systems, infact some times i programmed in assembly on a emulated dos machine ( i am to young for have a dos machine :( ) and i had to specify the size of the stack , but in modern systems i think that the stack is dinamically adjusted when you push or pop something into the stack, into the limits of the free space – Holeryn Aug 17 '20 at 17:17