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. Difference Between the Heap and The Stack

Difference Between the Heap and The Stack

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structuresperformance
6 Posts 5 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.
  • U Offline
    U Offline
    ursus zeta
    wrote on last edited by
    #1

    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?

    J L 2 Replies Last reply
    0
    • U ursus zeta

      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?

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Well... I guess you've already written some programs in C or C++, right? If so, unless you have used malloc or new, 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,b

      Get 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 a

      This 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

      C 1 Reply Last reply
      0
      • U ursus zeta

        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?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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

        U 1 Reply Last reply
        0
        • L Lost User

          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

          U Offline
          U Offline
          ursus zeta
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            Well... I guess you've already written some programs in C or C++, right? If so, unless you have used malloc or new, 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,b

            Get 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 a

            This 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

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            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

            D 1 Reply Last reply
            0
            • C Christian Graus

              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

              D Offline
              D Offline
              dabs
              wrote on last edited by
              #6

              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!

              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