About Memory Management When mentioning C++
When someone mention the memory management the c++ is capable of doing, how can i see this thing? is this done in theory like guessing? i took a logical design intro course and it covered the systems of numbers and boolean algebra and combinational logic,will this help? so say in Visual Studio , is there some kind of a tool to visualize the memory,i hope im not being ridiculous here ?
C++ has a variety of memory areas:
- space used for global and static variables, which is pre-allocated by the compiler
- "stack" memory that's used for preserving caller context during function calls, passing some function arguments (others may fit in CPU registers), and local variables
- "heap" memory that's allocated using new or new(C++'s preferred approach) or malloc (a lower-level function inherited from C), and released with delete, delete or free respectively.
The heap is important in that it supports run-time requests for arbitrary amounts of memory, and the usage persists until delete or free is explicitly used, rather than being tied to the lifetime of particular function calls as per stack memory.
I'm not aware of any useful tools for visualising and categorising the overall memory usage of a running C++ program, less still for relating that back to which pointers in the source code currently have how much memory associated with them. As a very general guideline, it's encouraged to write code in such a way that pointers are only introduced when the program is ready to point them at something, and they go out of scope when they will no longer pointer at something. When that's impractical, it can be useful to set them to NULL (0), so that if you're monitoring the executing program in a debugger you can tell the pointer isn't meant to point to legitimate data at that point.