Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "standard" size of the stack and the heap in a C program?

I have read that the "standard" and initial stack size on Linux is ~8MB and Windows is ~1MB.

But how does the heap allocation work? Does the OS set a "virtual" size to the process heap like it does for the stack with committed and reserved memory?

like image 819
Julio Vga Avatar asked Nov 05 '11 01:11

Julio Vga


2 Answers

Classically, the layout of a program has the 'text' (or 'code') segment at the low end of memory, followed by the fixed data ('data' and 'bss' segments), followed by a gap, with the stack growing downwards from high memory. The gap in the middle becomes the heap, which grows from the end of the data towards the stack.

Things are more complex with threaded programs, shared libraries loaded, shared memory, etc.

The initial stack size is o/s dependent. The initial heap size is logically zero, but tends to grow almost immediately (as the program and the shared libraries are loaded).

like image 117
Jonathan Leffler Avatar answered Oct 20 '22 11:10

Jonathan Leffler


There is no general "standard size". Individual operating systems will have a default size, but usually they can be altered with appropriate parameters in the program image or on the command line.

C executes in a vast range of systems from tiny microprocessors with only a few hundred bytes of available memory to gigantic processor arrays with hundreds of gigabytes.

In your larger systems (including most Windows and Linux environments) the stack and heap will be assigned to segments that can be expanded, so that physical memory for maximum sizes does not need to be pre-reserved. Many micros, though, lack memory mapping hardware and the sizes must be pre-reserved (though sometimes the stack and heap are made to grow towards each other so that there's only one common limit).

like image 45
Hot Licks Avatar answered Oct 20 '22 11:10

Hot Licks