Previous slide
Next slide
Toggle fullscreen
Open presenter view
Source:
https://courses.engr.illinois.edu/cs225/fa2022/resources/stack-heap/
Allocate a
Cube
width
20
on the heap, allocate a Cube pointer
c
on
CreateCubeOnHeap
's stack to store the address of the
Cube
.
Source:
https://courses.engr.illinois.edu/cs225/fa2022/resources/stack-heap/
Deallocate stack memory for
CreateCubeOnHeap
and return the value of pointer
c
.
Source:
https://courses.engr.illinois.edu/cs225/fa2022/resources/stack-heap/
Allocate
cube
on
main
's stack and store the returned pointer.
Source:
https://courses.engr.illinois.edu/cs225/fa2022/resources/stack-heap/
Call method
getVolume
on
cube
, which calculates the volume to be
8000
.
Source:
https://courses.engr.illinois.edu/cs225/fa2022/resources/stack-heap/
Allocate double
v
to store the return value
8000
.
Source:
https://courses.engr.illinois.edu/cs225/fa2022/resources/stack-heap/
Deallocate the
Cube
pointed by
cube
, notice that cube is
still pointing to invalid memory
on heap
Source:
https://courses.engr.illinois.edu/cs225/fa2022/resources/stack-heap/
Set the value of
cube
to
nullptr
, which is
0
.
Source:
https://courses.engr.illinois.edu/cs225/fa2022/resources/stack-heap/
Deallocate the stack memory of
main
.