How Can You Know if a Pointer Has Been Assigned Heap Memory
Stack and Heap Memory
past Jenny Chen, Ruohao GuoOverview
When a programme is running, it takes upward retentivity. Sometimes nosotros are non even aware of the memory being allocated. In fact, every fourth dimension yous create a new variable, your program is allocating more than memory for you to store that variable. This article focuses on ii kinds of memories: stack and heap.
General Retentiveness 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 retention for programmer to allocate -
data
: stores global variables, separated into initialized and uninitialized -
text
: stores the code being executed
In guild to pinpoint each memory location in a program'south memory, nosotros assign each byte of retention an "address". The addresses go from 0 all the way to the largest possible address, depending on the automobile. As the effigy below, the text
, information
, and heap
segments take depression accost numbers, while the stack
memory has college addresses.
Past convention, we express these addresses in base xvi numbers. For case, the smallest possible address is 0x00000000
(where the 0x means base of operations 16), and the largest possible address could be 0xFFFFFFFF
.
Stack
As shown above, the stack segment is near the elevation of memory with high address. Every time a function is called, the auto allocates some stack retentivity for it. When a new local variables is alleged, more than stack memory is allocated for that part to shop the variable. Such allocations make the stack grow downwards. Subsequently the function returns, the stack memory of this function is deallocated, which ways all local variables get 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 prove examples of what stack memory looks like when the corresponding code is run:
a
for principal
b
for primary
and store -3
c
for chief
and store 12345
p
for main
and store address of b
a
for hullo
and store 100
hullo
and render 100
to main
d
for master
and store 100
main
and render 0
Previous Next
Since the stack memory of a function gets deallocated afterwards the function returns, there is no guarantee that the value stored in those expanse volition stay the same. A common mistake is to return a pointer to a stack variable in a helper function. After the caller gets this arrow, the invalid stack retentivity tin can exist overwritten at anytime. The following figures demonstrate one example of such scenario. Assume there is a Cube
class that has methods getVolume
and getSurfaceArea
, every bit well as a private variable width
.
c
for CreateCube
CreateCube
and return address of c
c
for master
and store the returned value. Notice that the stack memory of CreateCube
is overwritten getVolume
and summate volume using the width of c
. Since the width of c
is corrupted, the book is also incorrect getVolume
. Allocate r
for main
to store the render value of getVolume
getSurfaceArea
and summate 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 render value of getSurfaceArea
chief
and return 0
Previous Next
Heap
In the previous section we saw that functions cannot return pointers of stack variables. To solve this result, yous tin either return by re-create, or put the value at somewhere more permanent than stack memory. Heap memory is such a place. Unlike stack retentiveness, heap retention is allocated explicitly by programmers and it won't be deallocated until information technology is explicitly freed. To allocate heap retentivity 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 beneath demonstrate what happens in both stack and heap when the corresponding code is executed:
0
on the heap, allocate p
on principal
's stack to store the address of the integer Cube
with default width xx
on the heap, allocate c1
on main
's stack to store the address of the Cube
c2
on main
'south stack and store a copy of c1
setLength
on c2
, changes the width of the Cube
pointed by both c1
and c2
master
and return 0
Previous Adjacent
You may detect in the in a higher place case that even at the finish of the programme, the heap retentivity is notwithstanding not freed. This is chosen a retentivity leak.
To free heap memory, use the key discussion delete
followed by the arrow to the heap retentiveness. Exist careful about the memory y'all freed. If y'all endeavour to utilise the pointers to those memory after y'all free them, it will crusade undefined behavior. To avert such problems, it is good practice to set the value of freed pointers to nullptr
immediately after delete
. Here is an example that correctly frees retentiveness 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 arrow getVolume
on cube
, which calculates the book to be 8000
v
to shop the render value 8000
Cube
pointed by cube
, notice that cube
is still pointing to invalid memory on heap cube
to nullptr
, which is 0
master
Previous Next
Source: https://courses.engr.illinois.edu/cs225/sp2022/resources/stack-heap/
0 Response to "How Can You Know if a Pointer Has Been Assigned Heap Memory"
Post a Comment