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. Memory Management in C++ - Understanding Stack and Heap in Detail.?

Memory Management in C++ - Understanding Stack and Heap in Detail.?

Scheduled Pinned Locked Moved C / C++ / MFC
c++data-structuresperformancetutorialquestion
5 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.
  • M Offline
    M Offline
    mbatra31
    wrote on last edited by
    #1

    Hi, I want to understand Stack, Heap and other Memory management in C++ in detail. How stack and heap are managed, how and when memory gets allocated from these areas. Any good book or any good tutorial about this.? Regards, Mbatra

    V D L 3 Replies Last reply
    0
    • M mbatra31

      Hi, I want to understand Stack, Heap and other Memory management in C++ in detail. How stack and heap are managed, how and when memory gets allocated from these areas. Any good book or any good tutorial about this.? Regards, Mbatra

      V Offline
      V Offline
      Vijjuuu
      wrote on last edited by
      #2

      Stack is place where local variables are allocated.( C++ run time environment is managing memory for the program) allocation and deallocation will be done by c++ runtime environment. Heap is place when you allocated memory dynamically. ( that means you are managing memory for your variables) you have deallocate the memory. Above is the fair difference about stack and heap i would recommend Programming Application for Microsoft windows by Jeffery Richter. ( if you are targeting your c++ to windows environment)

      L 1 Reply Last reply
      0
      • M mbatra31

        Hi, I want to understand Stack, Heap and other Memory management in C++ in detail. How stack and heap are managed, how and when memory gets allocated from these areas. Any good book or any good tutorial about this.? Regards, Mbatra

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Have you tried this?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

        1 Reply Last reply
        0
        • V Vijjuuu

          Stack is place where local variables are allocated.( C++ run time environment is managing memory for the program) allocation and deallocation will be done by c++ runtime environment. Heap is place when you allocated memory dynamically. ( that means you are managing memory for your variables) you have deallocate the memory. Above is the fair difference about stack and heap i would recommend Programming Application for Microsoft windows by Jeffery Richter. ( if you are targeting your c++ to windows environment)

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

          Vijjuuu. wrote:

          allocation and deallocation will be done by c++ runtime environment.

          There isnt a C++ run time, it is not C# or some other Java variant, so the stack allocation is managed by your code when it calls push and ret. (Ever looked at what your C++ code does in asenbler?) Heap of course is allocated by calls to malloc and free, or new and delete, or any of the other heap managemet funcs. You should probably mention static memory too, very usefull and often overlooked.

          ============================== Nothing to say.

          1 Reply Last reply
          0
          • M mbatra31

            Hi, I want to understand Stack, Heap and other Memory management in C++ in detail. How stack and heap are managed, how and when memory gets allocated from these areas. Any good book or any good tutorial about this.? Regards, Mbatra

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

            Stack memory is managed by your code when it calls push and ret which decrement and increment the stack pointer respectively. (This is of course assembler, which is what your C code ends up as). Heap is allocated and deallocated respectively by calls to malloc and free, or new and delete, or any of the other heap managemet funcs. You should be aware of static memory too. It is allocated for the program when it loads, and contains all the variables you declare as static and all global variables. It also contains all the strings you declare in your code, such as char data[] = "my string". Have a shufti here: http://en.wikipedia.org/wiki/Data_segment[^]

            ============================== Nothing to say.

            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