Question:
What are stack and heap in C++?
anonymous
2013-03-25 09:20:26 UTC
Also tell whether each program has its own stack in memory? If so, then does each function of a program has its own stack?
Three answers:
Jeroonk
2013-03-25 09:45:03 UTC
Every program has one stack (lets not get into multithreading) and one heap.



- The heap is a general place to store data, in no particular order. This is where objects created using "new" are added (or in C: using malloc). Because there is no inherent ownership to memory allocated on the heap, you must always "delete" (in C: free) these regions after you're done. The new C++11 standard also added a lot of new "managed pointers" in the header, which remember to do this for you.



- The stack is where local variables, function arguments and some additional information is stored for the duration of function calls to "keep track" of nested function calls. It is literally laid out like a (LIFO) stack in memory: each function call makes the stack grow to accommodate the function arguments, its return address and the local variables. So the stack is divided in segments, one for the function that is currently being run, one for its caller and one each for all the calling functions up to "main". These segments are called "stack frames" or "activation records".



If a function call is made, a process like the following happens:

1. The function arguments are pushed onto the stack.

2. The instruction address where the calling function should resume is pushed onto the stack. The calling function is suspended and the first instruction of the called function is executed (CALL).

4. The local variables of the called function are pushed onto the stack.



If a function is done, and returns, a process like the following happens:

1. The function removes its local variables from the stack.

2. The next value on top of the stack is the now the return address of the calling function. After the callee returns (RET), execution jumps back to this address and the calling function is resumed. The address is removed from the stack in the process.

3. The calling function removes the function arguments from the stack.
jake
2013-03-25 09:40:44 UTC
That is a great question! So few actually understand it enough to utilize it properly, or to know what I mean when I say "utilize it properly".



The stack can be thought of like a stack of plates. LIFO, last in first out. You "stack" plates one on top of the other, and you can't just remove a plate from the middle, you have to work your way down, starting from the top (the "last" one put onto it).



The heap is like it sounds, a heap of memory. It is often fragmented due to volatility and lack of persistence in data used in it. Not that fragmentation matters on RAM, considering it's speed across the FSB. Just trying to paint a picture for you. It's like a bail of hay, and your hand reaching inside this "heap of hay", is a pointer in c++ -- locating the exact set of data, or handful of specific hay strands as it were.



Using the keyword "new" will utilize the Heap, because it is freely available memory.





See here for a detailed understanding: http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap

And i recommend you read all of the replies, they each have very useful information regarding each.
anonymous
2016-11-01 03:48:15 UTC
you need to use the stack for small archives products that have a life-time that aligns with the scope their declared in. you need to use heap for vast products whose lifetime is autonomous of the scope their created in. i could notice that dynamic allocation is available the two on the heap by way of purposes like malloc and on the stack by way of purposes like _alloca. you may use that for small buffers whose length you don't be attentive to at assemble time. additionally available is static reminiscence, non-stack reminiscence allotted at assemble time. in case you have have been given some thing you be attentive to you purely desire considered one of and desires a life-time comparable to the life of the appliance then static is a sturdy determination.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...