Heap, stack?
-
I know what a heap is, I know what a stack is but how do they relate to each other? I'm trying to reach a better understanding of memory management and not being able to put them in context is really throwing me.I know the question is a bit vague but so is my understanding at this point.:-O To overcome everything is to reach an invisible height where users cease to exist
-
I know what a heap is, I know what a stack is but how do they relate to each other? I'm trying to reach a better understanding of memory management and not being able to put them in context is really throwing me.I know the question is a bit vague but so is my understanding at this point.:-O To overcome everything is to reach an invisible height where users cease to exist
Stack works on a push pop concept, heap is random. stack is used for temporary static variables, heap is used to create dynamic variables. Thats why you get mem leaks with: Blah* blah = new Blah; if no delete follows but never with Blah blah; whatever is pushed onto the stack at startup is removed automatically at the end of the current scope, block, function thingy. ;P "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
-
Stack works on a push pop concept, heap is random. stack is used for temporary static variables, heap is used to create dynamic variables. Thats why you get mem leaks with: Blah* blah = new Blah; if no delete follows but never with Blah blah; whatever is pushed onto the stack at startup is removed automatically at the end of the current scope, block, function thingy. ;P "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
-
Stack works on a push pop concept, heap is random. stack is used for temporary static variables, heap is used to create dynamic variables. Thats why you get mem leaks with: Blah* blah = new Blah; if no delete follows but never with Blah blah; whatever is pushed onto the stack at startup is removed automatically at the end of the current scope, block, function thingy. ;P "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
HockeyDude wrote: current scope, block, function thingy :laugh: :laugh: :laugh: Nish Nish was here, now Nish has gone; He left his soul, to turn you on; Those who knew Nish, knew him well; Those who didn't, can go to hell. I like to :jig: on the Code Project Sonork ID 100.9786 voidmain www.busterboy.org
-
HockeyDude wrote: current scope, block, function thingy :laugh: :laugh: :laugh: Nish Nish was here, now Nish has gone; He left his soul, to turn you on; Those who knew Nish, knew him well; Those who didn't, can go to hell. I like to :jig: on the Code Project Sonork ID 100.9786 voidmain www.busterboy.org
it 5:00 in the morning here Nish I'm at a loss for words. Later brutha! "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
-
HockeyDude wrote: stack is used for temporary static variables, heap is used to create dynamic variables. Thank you that clarifies everything! :rolleyes: To overcome everything is to reach an invisible height where users cease to exist
No worries be happy! *dances like Harry Bellafonte* Daaaaaaaaao DaaaaaaaaaOh.. DB developer and me want to go home. Left key, right key, left key type!!! It's almost daylight here, so i'm gonna quit singing... ;P Nite' "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
-
it 5:00 in the morning here Nish I'm at a loss for words. Later brutha! "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
HockeyDude wrote: it 5:00 in the morning here Nish I'm at a loss for words Aaaah :-) A fellow nocturnal creature. HockeyDude wrote: Later brutha! Yup. later! Nish Nish was here, now Nish has gone; He left his soul, to turn you on; Those who knew Nish, knew him well; Those who didn't, can go to hell. I like to :jig: on the Code Project Sonork ID 100.9786 voidmain www.busterboy.org
-
HockeyDude wrote: it 5:00 in the morning here Nish I'm at a loss for words Aaaah :-) A fellow nocturnal creature. HockeyDude wrote: Later brutha! Yup. later! Nish Nish was here, now Nish has gone; He left his soul, to turn you on; Those who knew Nish, knew him well; Those who didn't, can go to hell. I like to :jig: on the Code Project Sonork ID 100.9786 voidmain www.busterboy.org
Nish [BusterBoy] wrote: A fellow nocturnal creature. Yup like a racoon. "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
-
I know what a heap is, I know what a stack is but how do they relate to each other? I'm trying to reach a better understanding of memory management and not being able to put them in context is really throwing me.I know the question is a bit vague but so is my understanding at this point.:-O To overcome everything is to reach an invisible height where users cease to exist
Let's see if I can shed some more light on the subject. There are basically two locations where memory can be allocated in C++: on the stack or on the heap. The stack is used by the compiler mostly to allocate local variables whose size is well known at compile time. Every time you define a local variable (inside a function block), the compiler reserves a block of bytes for the size taken up by the variable. This is a very quick operation because all the compiler does is assign the address to the variable and move its "stack pointer" down by the size of the variable. This is called pushing the stack. When the variable goes out of scope the compiler does the opposite -- it pops the stack -- and the memory is reclaimed. Thus, memory allocated on the stack is automatically reclaimed by the compiler, and it's all very quick because it's all done in one location -- the end of the stack, if you will. The stack is also used whenever a function is called to hold its return address and its parameters (if any). The heap is quite different. It's typically used to allocate variables that you plan to keep longer than duration of a function block, or variables whose size is not known until run-time (such as dynamic arrays). In C++, memory is taken from the heap via the new operator (or via the global C function "malloc"). When this happens, the heap manager looks for a block of memory large enough to hold the contents of your variable and returns the beginning address of that block. This block of memory stays allocated until your program calls the delete operator on that memory (or "free" if malloc was used) or until the program ends. By the way, allocating memory on the heap is much slower than allocating from the stack, so the stack should be used if the variable will not be needed outside the block where it was allocated and its size is known at compile-time. In addition, since the stack's memory is automatically reclaimed, it requires less housekeeping on the part of the programmer (to prevent memory leaks). However, the stack should ONLY be used when the variable will not be referenced outside the block where it was allocated, otherwise you'll be left with an invalid memory address which when used will cause the program to crash. I hope to have cleared things up. Regards, Alvaro Behind a beautiful woman there's usually a guy who just couldn't wait to get rid of her.