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