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#
  4. What is the exact difference between heap and stack?

What is the exact difference between heap and stack?

Scheduled Pinned Locked Moved C#
questiondata-structureshelp
14 Posts 8 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.
  • A Offline
    A Offline
    atoi_powered
    wrote on last edited by
    #1

    I hope I don't bother anyone with my simple and newbie-look-a-like questions :) The title itself is clearly indicating what I want for the answer. So thanks for your help

    R D R A C 5 Replies Last reply
    0
    • A atoi_powered

      I hope I don't bother anyone with my simple and newbie-look-a-like questions :) The title itself is clearly indicating what I want for the answer. So thanks for your help

      R Offline
      R Offline
      RobCroll
      wrote on last edited by
      #2

      In non technical teams the stack is an optimized memory bank that is very fast. The heap is a memory bank that isn't as fast but great for handling objects of varying sizes. So when you create an object, .NET looks to see if it is a native or reference type. If it is a native type, the value it put on the stack, otherwise it is put on the heap. The reason for this is that the stack only handles objects that are 32 bits in size. The fact that all memory allocations are the same size is what makes it fast. Also it's not a pointer to a value that is stored, it's the actual value itself. Because of this Boolean values actually occupy 4 bytes of memory. Native types like doubles are stored in 2 x 4 bytes A reference type like a string for example could be any size. It could also be very large. So what happens is the string is stored on the heap and a pointer is created and put on the stack so it can be found quickly. Either way, the combination offers a lot faster solution than a simple memory model.

      "You get that on the big jobs."

      OriginalGriffO 1 Reply Last reply
      0
      • A atoi_powered

        I hope I don't bother anyone with my simple and newbie-look-a-like questions :) The title itself is clearly indicating what I want for the answer. So thanks for your help

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        The stack is (NOT OPTIMIZED VERY FAST MEMORY!) just an area in memory where various values are stored in a Last In, First Out fashion. It's used to hold simple variable values, pointers to objects, parameters for methods calls, return addresses for returning control back to where a method was called from, ... A heap is a block of memory that contains both free (reserved for your app but unused) memory and allocated objects such as buffers, class instances, strings, or other complex types.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        1 Reply Last reply
        0
        • R RobCroll

          In non technical teams the stack is an optimized memory bank that is very fast. The heap is a memory bank that isn't as fast but great for handling objects of varying sizes. So when you create an object, .NET looks to see if it is a native or reference type. If it is a native type, the value it put on the stack, otherwise it is put on the heap. The reason for this is that the stack only handles objects that are 32 bits in size. The fact that all memory allocations are the same size is what makes it fast. Also it's not a pointer to a value that is stored, it's the actual value itself. Because of this Boolean values actually occupy 4 bytes of memory. Native types like doubles are stored in 2 x 4 bytes A reference type like a string for example could be any size. It could also be very large. So what happens is the string is stored on the heap and a pointer is created and put on the stack so it can be found quickly. Either way, the combination offers a lot faster solution than a simple memory model.

          "You get that on the big jobs."

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Reason for my vote of one: (and I suspect for the others) Wrong. It is so completely wrong, that there is almost nothing in there that is actually correct. I don't often give down votes, but I'm making an exception in this case! Sorry, but you really need to go back to the beginning and start learning this stuff again.

          Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          R A 2 Replies Last reply
          0
          • OriginalGriffO OriginalGriff

            Reason for my vote of one: (and I suspect for the others) Wrong. It is so completely wrong, that there is almost nothing in there that is actually correct. I don't often give down votes, but I'm making an exception in this case! Sorry, but you really need to go back to the beginning and start learning this stuff again.

            Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

            R Offline
            R Offline
            RobCroll
            wrote on last edited by
            #5

            I read Jeffrey Richter's book on CLR but I must have totally misunderstood what I was reading. Hopefully someone will explain the difference in this topic. At the moment I'm aware I've got it wrong. I just don't know why.

            "You get that on the big jobs."

            1 Reply Last reply
            0
            • A atoi_powered

              I hope I don't bother anyone with my simple and newbie-look-a-like questions :) The title itself is clearly indicating what I want for the answer. So thanks for your help

              R Offline
              R Offline
              Roger Wright
              wrote on last edited by
              #6

              This uesd to be a lot easier question to answer - the stack was a small bit of RAM pointed to by the CPU Stack Pointer (SP) register, which made it fast and easy to access. Every CALL instruction pushed the current values in all CPU registers onto the stack, then popped them back when the CALLed routine was finished to resume execution. The Heap was a block reserved in RAM by an application to hold and access run-time data. Now it's a bit more complex, but this[^] discussion puts it in perspective.

              Will Rogers never met me.

              1 Reply Last reply
              0
              • A atoi_powered

                I hope I don't bother anyone with my simple and newbie-look-a-like questions :) The title itself is clearly indicating what I want for the answer. So thanks for your help

                A Offline
                A Offline
                Abhinav S
                wrote on last edited by
                #7

                Have a look at Six important .NET concepts: Stack, heap, value types, reference types, boxing, and unboxing[^].

                Build your own survey - http://www.factile.net

                1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  Reason for my vote of one: (and I suspect for the others) Wrong. It is so completely wrong, that there is almost nothing in there that is actually correct. I don't often give down votes, but I'm making an exception in this case! Sorry, but you really need to go back to the beginning and start learning this stuff again.

                  Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                  A Offline
                  A Offline
                  atoi_powered
                  wrote on last edited by
                  #8

                  I understand what you are trying to say but I didn't have another choice and I thought this is the right place to ask that problem. I already excused for my question you consider non-advanced and unprofessional :) . I hope you realize why. ;)

                  P OriginalGriffO 2 Replies Last reply
                  0
                  • A atoi_powered

                    I understand what you are trying to say but I didn't have another choice and I thought this is the right place to ask that problem. I already excused for my question you consider non-advanced and unprofessional :) . I hope you realize why. ;)

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #9

                    You do realise that OG wasn't addressing you don't you? What he was saying is that the answer you were given was wrong - not that you were stupid for asking the question. It's actually a pretty fair question, as the concepts are misunderstood, which you can see from the answers.

                    *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                    "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                    CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                    A 1 Reply Last reply
                    0
                    • P Pete OHanlon

                      You do realise that OG wasn't addressing you don't you? What he was saying is that the answer you were given was wrong - not that you were stupid for asking the question. It's actually a pretty fair question, as the concepts are misunderstood, which you can see from the answers.

                      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                      A Offline
                      A Offline
                      atoi_powered
                      wrote on last edited by
                      #10

                      Yeah your right, I didn't recognize! Oops :D

                      1 Reply Last reply
                      0
                      • A atoi_powered

                        I understand what you are trying to say but I didn't have another choice and I thought this is the right place to ask that problem. I already excused for my question you consider non-advanced and unprofessional :) . I hope you realize why. ;)

                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #11

                        No, no, you misunderstood - that was not directed at you or your question (which is a good, sensible question). That is why it was a reply to RobCroll and not to you, and why his answer is light grey, and yours is still blue. His answer to your question was wrong in just about every respect, and what he said you should ignore - look at Dave Kreskowiak's answer which is much, much better. That is why I (and I suspect others) voted him down - the advice was bad. If you look at the bottom left corner of a message you will see a "rate this message" facility. Have a look at his answer: 1.71/5 (7 votes) indicating he has had seven ratings, and that they have generally been unhappy. A vote of one is a "this is bad", a vote of 5 is a "this is good". If your message has no votes, it is blue, if it is down voted, it goes grey. If it is upvoted it goes green.

                        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        A 1 Reply Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          No, no, you misunderstood - that was not directed at you or your question (which is a good, sensible question). That is why it was a reply to RobCroll and not to you, and why his answer is light grey, and yours is still blue. His answer to your question was wrong in just about every respect, and what he said you should ignore - look at Dave Kreskowiak's answer which is much, much better. That is why I (and I suspect others) voted him down - the advice was bad. If you look at the bottom left corner of a message you will see a "rate this message" facility. Have a look at his answer: 1.71/5 (7 votes) indicating he has had seven ratings, and that they have generally been unhappy. A vote of one is a "this is bad", a vote of 5 is a "this is good". If your message has no votes, it is blue, if it is down voted, it goes grey. If it is upvoted it goes green.

                          Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                          A Offline
                          A Offline
                          atoi_powered
                          wrote on last edited by
                          #12

                          You're right! Thanks :)

                          OriginalGriffO 1 Reply Last reply
                          0
                          • A atoi_powered

                            You're right! Thanks :)

                            OriginalGriffO Offline
                            OriginalGriffO Offline
                            OriginalGriff
                            wrote on last edited by
                            #13

                            You're welcome!

                            Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                            1 Reply Last reply
                            0
                            • A atoi_powered

                              I hope I don't bother anyone with my simple and newbie-look-a-like questions :) The title itself is clearly indicating what I want for the answer. So thanks for your help

                              C Offline
                              C Offline
                              Cracked Down
                              wrote on last edited by
                              #14

                              Finally some one has answered!!! ;P ;P ;P ;P

                              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