Download presentation
Presentation is loading. Please wait.
1
Dynamic Memory A whole heap of fun…
2
Review: The Stack C++ allocates variables on a stack
All memory that might be used in function allocated at one time Address Identifier Value 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100
3
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(6); int z = 5; bar(); } Address Identifier Value 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100
4
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 ?? 115 114 113 112 111 z 110 109 108 107 y 106 105 104 103 X 102 101 100
5
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 ?? 115 114 113 112 111 z 110 109 108 107 y 106 105 104 103 X 10 102 101 100
6
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 ?? 115 114 113 112 111 z 110 109 108 107 y 1 106 105 104 103 X 10 102 101 100
7
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 c ?? 115 q 10 114 113 112 111 z 110 109 108 107 y 1 106 105 104 103 X 102 101 100
8
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 c 'a' 115 q 10 114 113 112 111 z ?? 110 109 108 107 y 1 106 105 104 103 X 102 101 100
9
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 'a' 115 10 114 113 112 111 z ?? 110 109 108 107 y 1 106 105 104 103 X 102 101 100
10
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 'a' 115 10 114 113 112 111 z 5 110 109 108 107 y 1 106 105 104 103 X 102 101 100
11
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 'a' 115 x 10 114 113 112 111 z 5 110 109 108 107 y 1 106 105 104 103 X 102 101 100
12
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 'a' 115 x 8 114 113 112 111 z 5 110 109 108 107 y 1 106 105 104 103 X 10 102 101 100
13
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 'a' 115 8 114 113 112 111 z 5 110 109 108 107 y 1 106 105 104 103 X 10 102 101 100
14
Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 'a' 115 8 114 113 112 111 5 110 109 108 107 1 106 105 104 103 10 102 101 100
15
What will this do? getPointerToTen Initialize a variable
Makes a pointer to it Returns that pointer Main prints twice
16
The Stack C++ allocates variables on a stack
int* getPointerToTen() { int x = 10; int* px = &x; return px; } int main() { int* pTen = getPointerToTen(); cout << *pTen << endl; } Address Identifier Value 116 115 114 113 112 111 110 109 108 107 106 105 104 103 pTen ?? 102 101 100
17
The Stack C++ allocates variables on a stack
int* getPointerToTen() { int x = 10; int* px = &x; return px; } int main() { int* pTen = getPointerToTen(); cout << *pTen << endl; } Address Identifier Value 116 115 114 113 112 111 px 104 110 109 108 107 x 10 106 105 103 pTen ?? 102 101 100
18
The Stack C++ allocates variables on a stack
int* getPointerToTen() { int x = 10; int* px = &x; return px; } int main() { int* pTen = getPointerToTen(); cout << *pTen << endl; } Address Identifier Value 116 115 114 113 112 111 104 110 109 108 107 10 106 105 103 pTen 102 101 100
19
??? Pointers to items on stack may go bad
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.