Difference Between the Heap and The Stack
-
I'm completely new to programming, so this question will probably provide some laughs for you experienced people. What is the difference between the Heap and the Stack? And is this difference addressed by any code techniques? In other words, is this important, or does Windows manage memory without having to make the distinction? Another question; When you guys refer to a memory leak, is this exclusively 'heap leak' or 'stack leak', or does it even matter? And, are these locations assignable?
-
I'm completely new to programming, so this question will probably provide some laughs for you experienced people. What is the difference between the Heap and the Stack? And is this difference addressed by any code techniques? In other words, is this important, or does Windows manage memory without having to make the distinction? Another question; When you guys refer to a memory leak, is this exclusively 'heap leak' or 'stack leak', or does it even matter? And, are these locations assignable?
Well... I guess you've already written some programs in C or C++, right? If so, unless you have used
malloc
ornew
, all the variables declared and used in your program live on the stack. They're automatically added and removed on a stack-like manner as the flow of execution progress. For instance:int a=0; //stack: a
int b=10; //stack: a,b
for(a=0;<b;++a){
int i=2*a; //stack: a,b,i
}
// stack: a,bGet the idea? The lifespan of stack variables is automatically taken care of by the compiler. Sometimes, however, you need to have a variable that lives longer that the context it is declared in. These can be seen as "floating" variables whose birth and death is under control of the programmer, not the compiler --you decide when the variable is created, and when it is destroyed. The space these variables live in is called the heap. In C/C++, heap variables are handled though pointers:
int * a; // pointer to int ("heap-based" int, if you prefer)
...
void f()
{
a=new int; // a is created, but will survive to termination of f}
...
delete a; // the program decides now to remove aThis is a very shallow introduction to the concepts, any basic C++ tutorial will explain these things better and in more detail. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
I'm completely new to programming, so this question will probably provide some laughs for you experienced people. What is the difference between the Heap and the Stack? And is this difference addressed by any code techniques? In other words, is this important, or does Windows manage memory without having to make the distinction? Another question; When you guys refer to a memory leak, is this exclusively 'heap leak' or 'stack leak', or does it even matter? And, are these locations assignable?
When you do int* a = new int; *a = 5; the memory for the variable is on the heap When you do int a; a= 5; the memory for the variable is on the stack In the first case, you have to call delete a; otherwise, it will cause a memory leak. When I say memory leak, it always refers to a leak on the heap. My article on a reference-counted smart pointer that supports polymorphic objects and raw pointers
-
When you do int* a = new int; *a = 5; the memory for the variable is on the heap When you do int a; a= 5; the memory for the variable is on the stack In the first case, you have to call delete a; otherwise, it will cause a memory leak. When I say memory leak, it always refers to a leak on the heap. My article on a reference-counted smart pointer that supports polymorphic objects and raw pointers
Joaquín M López Muñoz and Thomas George, Thanks much, this is just what I was looking for. I'll pursue those clues. And, T. George, thanks for the article.
-
Well... I guess you've already written some programs in C or C++, right? If so, unless you have used
malloc
ornew
, all the variables declared and used in your program live on the stack. They're automatically added and removed on a stack-like manner as the flow of execution progress. For instance:int a=0; //stack: a
int b=10; //stack: a,b
for(a=0;<b;++a){
int i=2*a; //stack: a,b,i
}
// stack: a,bGet the idea? The lifespan of stack variables is automatically taken care of by the compiler. Sometimes, however, you need to have a variable that lives longer that the context it is declared in. These can be seen as "floating" variables whose birth and death is under control of the programmer, not the compiler --you decide when the variable is created, and when it is destroyed. The space these variables live in is called the heap. In C/C++, heap variables are handled though pointers:
int * a; // pointer to int ("heap-based" int, if you prefer)
...
void f()
{
a=new int; // a is created, but will survive to termination of f}
...
delete a; // the program decides now to remove aThis is a very shallow introduction to the concepts, any basic C++ tutorial will explain these things better and in more detail. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
You know, this is the one thing I never remember. I know the concepts fine, but which is the stack and which is the heap is something I can never recall. I think you've cracked it for me. A stack is more orderly than a heap, and the heap is disorderly unless the programmer cleans it up. Well done, thank you. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002 Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002
-
You know, this is the one thing I never remember. I know the concepts fine, but which is the stack and which is the heap is something I can never recall. I think you've cracked it for me. A stack is more orderly than a heap, and the heap is disorderly unless the programmer cleans it up. Well done, thank you. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002 Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002
Maybe it helps me that English isn't my native language but since the word "heap" does not correlate directly to an object in the real world in my mind then it's only a matter of connecting the stack to one such thing. And visualizing a stack of plates in a cafeteria usually does it for me. Then there are the HeapXXX( ) functions which should also be a hint :-)
Wenn ist das Nunstück git und Slotermeyer? Ja! Beierhund das oder die Flipperwaldt gersput!