Dynamic Memory And Objects
What will this do? What is the difference between these two samples?
The Stack Traditional model: Stack grows down in memory CODE GLOBALS
The Stack Traditional model: Stack grows down in memory Each function adds a Stack Frame : new set of local variables CODE GLOBALS STACK STACK FRAME
The Stack Traditional model: Stack grows down in memory Each function adds a Stack Frame : new set of local variables Exiting a function removes a stack frame CODE GLOBALS STACK STACK FRAME
The Heap Heap is memory managed by OS and programmer C++ program calls new to ask for heap space CODE GLOBALS STACK STACK FRAME HEAP Stays until explicitly freed
The Heap Heap is unaffected by changes to stack CODE GLOBALS STACK
The Heap Heap is unaffected by changes to stack CODE GLOBALS STACK HEAP Stays until explicitly freed
Objects on Stack Objects created without new are on the stack: 2000 p1 Address Identifier Value 2000 p1 Point 1996 1992 1988 1984 1980 1976 … 1000 996 992 988 984 980
Objects on Heap Objects created with new are on heap Use pointer to keep track of Address Identifier Value 2000 pp 1000 1996 1992 1988 1984 1980 1976 … Point 996 992 988 984 980
Guidance Can use pointer to object on either stack or heap For now You MUST use pointers for heap objects For now Put objects on stack