Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Heap, stack?

Heap, stack?

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structuresperformance
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jasmyn
    wrote on last edited by
    #1

    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

    A A 2 Replies Last reply
    0
    • J Jasmyn

      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

      A Offline
      A Offline
      alex barylski
      wrote on last edited by
      #2

      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

      J N 2 Replies Last reply
      0
      • A alex barylski

        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

        J Offline
        J Offline
        Jasmyn
        wrote on last edited by
        #3

        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

        A 1 Reply Last reply
        0
        • A alex barylski

          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

          N Offline
          N Offline
          Nish Nishant
          wrote on last edited by
          #4

          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

          A 1 Reply Last reply
          0
          • N Nish Nishant

            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

            A Offline
            A Offline
            alex barylski
            wrote on last edited by
            #5

            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

            N 1 Reply Last reply
            0
            • J Jasmyn

              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

              A Offline
              A Offline
              alex barylski
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • A alex barylski

                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

                N Offline
                N Offline
                Nish Nishant
                wrote on last edited by
                #7

                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

                A 1 Reply Last reply
                0
                • N Nish Nishant

                  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

                  A Offline
                  A Offline
                  alex barylski
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • J Jasmyn

                    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

                    A Offline
                    A Offline
                    Alvaro Mendez
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups