How to Clear Continuous Memory 7 3
Stack and Heap Memory
by Jenny Chen, Ruohao GuoOverview
When a program is running, it takes up memory. Sometimes we are not even aware of the memory being allocated. In fact, every time you create a new variable, your program is allocating more memory for you to store that variable. This article focuses on two kinds of memories: stack and heap.
General Memory Layout
Each running program has its own memory layout, separated from other programs. The layout consists of a lot of segments, including:
-
stack: stores local variables -
heap: dynamic memory for programmer to allocate -
data: stores global variables, separated into initialized and uninitialized -
text: stores the code being executed
In order to pinpoint each memory location in a program's memory, we assign each byte of memory an "address". The addresses go from 0 all the way to the largest possible address, depending on the machine. As the figure below, the text, data, and heap segments have low address numbers, while the stack memory has higher addresses.
By convention, we express these addresses in base 16 numbers. For instance, the smallest possible address is 0x00000000 (where the 0x means base 16), and the largest possible address could be 0xFFFFFFFF.
Stack
As shown above, the stack segment is near the top of memory with high address. Every time a function is called, the machine allocates some stack memory for it. When a new local variables is declared, more stack memory is allocated for that function to store the variable. Such allocations make the stack grow downwards. After the function returns, the stack memory of this function is deallocated, which means all local variables become invalid. The allocation and deallocation for stack memory is automatically done. The variables allocated on the stack are called stack variables, or automatic variables.
The following figures show examples of what stack memory looks like when the corresponding code is run:
a for main b for main and store -3 c for main and store 12345 p for main and store address of b a for hello and store 100 hello and return 100 to main d for main and store 100 main and return 0 Previous Next
Since the stack memory of a function gets deallocated after the function returns, there is no guarantee that the value stored in those area will stay the same. A common mistake is to return a pointer to a stack variable in a helper function. After the caller gets this pointer, the invalid stack memory can be overwritten at anytime. The following figures demonstrate one example of such scenario. Assume there is a Cube class that has methods getVolume and getSurfaceArea, as well as a private variable width.
c for CreateCube CreateCube and return address of c c for main and store the returned value. Notice that the stack memory of CreateCube is overwritten getVolume and calculate volume using the width of c. Since the width of c is corrupted, the volume is also incorrect getVolume. Allocate r for main to store the return value of getVolume getSurfaceArea and calculate surface area using the width of c. Similar to getVolume, the surface area calculated will be incorrect getSurfaceArea. Allocate v for main to store the return value of getSurfaceArea main and return 0 Previous Next
Heap
In the previous section we saw that functions cannot return pointers of stack variables. To solve this issue, you can either return by copy, or put the value at somewhere more permanent than stack memory. Heap memory is such a place. Unlike stack memory, heap memory is allocated explicitly by programmers and it won't be deallocated until it is explicitly freed. To allocate heap memory in C++, use the keyword new followed by the constructor of what you want to allocate. The return value of new operator will be the address of what you just created (which points to somewhere in the heap).
The figures below demonstrate what happens in both stack and heap when the corresponding code is executed:
0 on the heap, allocate p on main's stack to store the address of the integer Cube with default width 20 on the heap, allocate c1 on main's stack to store the address of the Cube c2 on main's stack and store a copy of c1 setLength on c2, changes the width of the Cube pointed by both c1 and c2 main and return 0 Previous Next
You may notice in the above example that even at the end of the program, the heap memory is still not freed. This is called a memory leak.
To free heap memory, use the key word delete followed by the pointer to the heap memory. Be careful about the memory you freed. If you try to use the pointers to those memory after you free them, it will cause undefined behavior. To avoid such issues, it is good practice to set the value of freed pointers to nullptr immediately after delete. Here is an example that correctly frees memory after using it.
Cube width 20 on the heap, allocate a Cube pointer c on CreateCubeOnHeap's stack to store the address of the Cube CreateCubeOnHeap and return the value of pointer c cube on main's stack and store the returned pointer getVolume on cube, which calculates the volume to be 8000 v to store the return value 8000 Cube pointed by cube, notice that cube is still pointing to invalid memory on heap cube to nullptr, which is 0 main Previous Next
Source: https://courses.engr.illinois.edu/cs225/fa2022/resources/stack-heap/
0 Response to "How to Clear Continuous Memory 7 3"
Post a Comment