7

In C99, the following line of code create a variable ptr on stack which points to a memory region on the heap.

int *ptr = (int*)malloc(sizeof(int)*10);

Where are the definitions of stack and heap? I could not find them in the C99 language specification.

Are the stack and heap defined by the operating system or instruction set architecture or something else?

The another related questions is that whether the concept of stack and heap in C# are exactly the same as the concept in C99? Since C# code are run on the .Net framework, I do not sure if the concept is the same as C99.

trincot
  • 317,000
  • 35
  • 244
  • 286
mingpepe
  • 489
  • 5
  • 10
  • This will help you partially. Has a nice answer. http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap – Nikhil Vartak Nov 26 '15 at 05:35
  • Thanks for your information. However the main question is that where are the definitions? – mingpepe Nov 26 '15 at 05:42
  • In C, "stack" and "heap" are colloquialisms. – user253751 Nov 26 '15 at 05:54
  • 2
    There are no definitions in the language for the stack and heap. These are concepts to do with how the language allocates memory. Compare the addresses that you get back from mallloc with the address of some local variables `int i=0; int * pi = &i;`. You'll find that the local variables are in a different area of memory (stack) to the malloc'd memory (heap). – Galax Nov 26 '15 at 05:59

2 Answers2

6

Stacks and heaps are implementation details; like you discovered, the C language definition doesn't mention them at all.

The C language definition talks about storage durations of objects. Objects with auto storage duration have lifetimes that extend over their enclosing block; it just so happens that the hardware stack makes that behavior easy to implement, so almost all C implementations do so. Objects with allocated storage duration have lifetimes that extend from the malloc/calloc/realloc call until a call to free. Again, almost all C implementations take advantage of the system heap for that behavior.

However, an implementation doesn't have to use a system-supplied stack or heap to satisfy object storage duration requirements; it would just be a bit more work.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • What is the system-supplied stack or heap? In the view point of hardware, they are the same thing right? So are they defined by the operating system? – mingpepe Nov 26 '15 at 09:14
  • 1
    @mingpepe - the stack is usually defined by the hardware platform (most CPUs I'm familiar with devote a couple of registers to stack management). Heaps are mixed between hardware and OS. Implementations differ. – John Bode Nov 26 '15 at 12:33
  • What does it mean that heaps are mixed between hardware and OS? – mingpepe Nov 26 '15 at 13:58
  • 1
    @mingpepe: meaning that utilities provided by the OS are responsible for managing items in the heap, but that the hardware *may* provide specialized storage. – John Bode Nov 26 '15 at 17:26
1

The heap is the amount of memory that is allocated to a given process that is running on the machine. The stack is a generally smaller amount of memory that is allocated to the thread that is currently running on the given process.

When you create a local variable it is stored to the stack. This selection of memory is called the stack because as it deals with scopes different values are pushed or popped from the addressable space as you would do with a stack data structure.

Then when you malloc a variable it is stored to the heap and thus saved even across multiple scopes.

Note that things stored on the heap must be free'd when you're done using them, whereas the OS automatically handles that for things on the stack.

checkout http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html

akohlbre
  • 3
  • 2
pwilmot
  • 586
  • 2
  • 8