Stack memory vs heap memory

Possible duplicate:
What and where are the stack and heap

I am programming in C ++ and I am always curious about what stack memory and heap memory are. All I know is when I call new, I get memory from the heap. If, when creating local variables, I get memory from the stack. After some research on the Internet, the most common answer is temporary stack memory, and heap memory is constant.

Is the stack and heap memory model a concept for an operating system or computer architecture? Thus, some of them may not follow the memory model on the stack and heap, or all of them follow it?

Stack and heap memory is an abstraction of the virtual memory model (which can change the memory between a disk and RAM). So, can both the stack and the heap memory physically be RAM or disk? Then what is the reason the heap allocation seems slower than the stack match?

Also, will the main program be executed on the stack or heap?

Also, what happens if the process ends from the stack memory or the allocated heap memory?

thank

+48
c ++ memory
Apr 29 '11 at 19:09
source share
3 answers

Stack memory is, in particular, the area of ​​memory accessible through the processor stack register. Stack was used as a way to implement the Jump-Subroutine - Return code template in assembly language, and also as a means of implementing interrupt handling at the hardware level. For example, during an interrupt, Stack was used to store various CPU registers, including Status (which indicates the results of the operation) and the program counter (where the CPU was in the program when the interrupt occurred).

Stack memory is very important for regular CPU design. The speed of its distribution / release is fast, because it is strictly the latest design. This is a simple matter of move operations and reduce / increase operations in the Stack register.

The heap memory was simply the memory remaining after loading the program, and the stack memory was allocated. It may (or may not) include a global variable space (this is a matter of convention).

A modern proactive multi-tasking OS with virtual memory and devices with memory mapping makes the real situation more complicated, but this stack is against the heap in a nutshell.

+24
Apr 29 '11 at 19:24
source share

In C ++, stack memory contains local variables that are stored / created. The stack is also used to store parameters passed to functions.

The stack is similar to the std :: stack class, you push the parameters on it, and then call the function. Then the function knows what parameters it expects, can be found at the end of the stack. Similarly, a function can push local residents to the stack and pop out of it before returning from the function. (optimization of the caveat compiler and calling conventions - all this is not so simple)

The stack is best understood from a low level, and I would recommend this link Art of Assembly - Passing parameters on the stack . It’s rare if someday you will consider some kind of manual stack manipulation from C ++.

In general, a stack is preferable because it is usually located in the CPU cache, so operations with objects stored on it tend to be faster. However, the stack is a limited resource and should not be used for anything large. Running from stack memory is called stack buffer overflow . This is a serious thing to come across, but you really shouldn't run into it unless you have a crazy recursive function or something like that.

Heap memory is very important, as rskar says. Generally speaking, C ++ objects allocated by the new, or memory blocks allocated using malloc, fall into the heap. Heap memory should almost always be freed manually, although you should use a smart pointer class or similar to avoid having to keep that in mind. Running from heap memory can (will?) Result in std :: bad_alloc.

+18
Apr 29 '11 at 19:34
source share

This is an abstraction of the language - in some languages ​​there is both one and some.

In the case of C ++, the code does not run on the stack or on the heap. You can check what happens if you run out of heap memory by repeating the new call to allocate memory in a loop without calling delete to free it. But backup your system before doing this .

+2
Apr 29 '11 at 19:14
source share



All Articles