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.

4
  • This will help you partially. Has a nice answer. stackoverflow.com/questions/79923/… Commented Nov 26, 2015 at 5:35
  • Thanks for your information. However the main question is that where are the definitions? Commented Nov 26, 2015 at 5:42
  • In C, "stack" and "heap" are colloquialisms. Commented Nov 26, 2015 at 5: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). Commented Nov 26, 2015 at 5:59

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 - 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.
What does it mean that heaps are mixed between hardware and OS?
@mingpepe: meaning that utilities provided by the OS are responsible for managing items in the heap, but that the hardware may provide specialized storage.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.