3

It's my understanding that on Linux the process loader doesn't automatically create a "heap" region for a process like it's the case for the stack, is that correct?

I used /proc/$$/maps and before the call to malloc() there was no "heap" region.

4

Linux does not "automatically create" a region for both, heap and stack segments.

In programming languages there is always an entry point, where control is transferred from the operating system to the program. In C, this is the main() function.

Each process in Linux has a memory mapping of 4GB in 32-Bit and 8TB in 64-Bit environments. This is the maximum amout of memory that is possible to address by the operating system. Notice, that this is independent of how many physical memory your system really has. Every process is giving the illusion that it is alone on the computer.

Before calling main(), the operating system pushes the elements of the command line, that was used to invoke the program, on "top" of the initially empty stack, this is where the stack segment is. If the main() calls itself a function the passing arguments are pushed in the stack segment.

 4GB/8TB┌───────────┐
        │   stack   │ <- main(), argc/argv parameters and functions, growing downwards
        ├ ─ ─ ─ ─ ─ ┤
        │           │
        │           │ <- empty
        │           │
        │           │
        ├───────────┤
        │   data    │ <- static data and literal constants
        ├───────────┤
        │   text    │ <- all the instructions; the compiled code
       0└───────────┘

Now your question:

I used /proc/$$/maps and before the call to malloc there was no "heap" region.

The heap memory: A process can expand into the unoccupied portion of virtual memory at runtime using the malloc() system call. This dynamically allocated memory is located above data in the heap segment. So it is normal, that there is no heap region or segment until the first malloc() call.

 4GB/8TB┌───────────┐
        │   stack   │ <- main(), argc/argv parameters and functions, growing downwards
        ├ ─ ─ ─ ─ ─ ┤
        │           │
        │           │ <- empty
        ├ ─ ─ ─ ─ ─ ┤
        │   heap    │ <- malloc()'d memory space, growing upwards
        ├───────────┤
        │   data    │ <- static data and literal constants
        ├───────────┤
        │   text    │ <- all the instructions; the compiled code
       0└───────────┘
  • is the memory mapped region part of the heap? – Nikos Jul 11 '17 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.