Stack and heap in a programming language

In C99, the following line of code creates the ptr variable on the stack, which points to the area of โ€‹โ€‹memory on the heap.

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

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

Are the stack and heap specific to the architecture of the operating system or set of instructions or something else?

Other related questions are whether the concepts of stack and heap in C # are exactly the same as the concepts in C99? Since C # code runs in a .Net framework, I'm not sure the concept is the same as C99.

+7
c stack heap c #
source share
2 answers

Stacks and heaps are implementation details; as you discovered, the C language definition does not mention them at all.

The definition of the C language indicates the duration of the storage of objects. Objects with auto storage time have a lifetime that extends to their blocking block; it just happens that the hardware stack makes this behavior easy to implement, so almost all C implementations do this. Objects with allocated storage allocated have a lifetime that extends from calling malloc / calloc / realloc to calling free . Again, almost all C implementations take advantage of the system heap for this behavior.

However, to implement the requirements for storing objects, it is not necessary to use the stacks or heaps provided by the system; it will be a little more work.

+6
source share

A heap is the amount of memory that is allocated to a given process running on a machine. The stack usually represents the smaller amount of memory that is allocated to the thread that is currently running in this process.

When creating a local variable, it is saved on the stack. This memory choice is called the stack, because since it deals with regions, various values โ€‹โ€‹are popped or pushed out of the address space, as would be the case with the stack data structure.

Then, when you are a malloc variable, it is stored on the heap and thus is saved even in several areas.

Note that things stored on the heap should be free when you finish using them, while the OS will automatically handle this for things on the stack.

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

+1
source share

All Articles