Memory Model CS 2022 Introduction to C Instructor Hussam Abu Libdeh based on slides by Saikat Guha Fall 2011 Lecture 4 Memory Model CS 2022 Fall 2011 Lecture 4 Memory I I Program code Function variables I I I I Arguments Local variables Return location Global Variables I I Statically Allocated Dynamically Allocated addr 0xFFFFFFFF Function Stack Frame main return addr fact 5 fact 4 Local Var 2 Local Var 3 Local Var 4 Global Dynamic Heap Global Static Arg 3 next fn Arg 2 next fn Code addr 0x00000000 Memory Model Local Var 1 CS 2022 Fall 2011 Lecture 4 Arg 1 next fn The Stack Stores I I I I Function local variables Temporary variables Arguments for next function call Where to return when function ends Memory Model CS 2022 Fall 2011 Lecture 4 The Stack Managed by compiler I I I I One stack frame each time function called Created when function called Stacked on top under one another Destroyed at function exit Memory Model CS 2022 Fall 2011 Lecture 4 The Stack int fact int n int res if n 1 return 1 res fact n 1 return n res main int main int res fact 5 return 0 Memory Model CS 2022 Fall 2011 Lecture 4 The Stack int fact int n int res if n 1 return 1 res fact n 1 return n res main 5 arg int main int res fact 5 return 0 Memory Model CS 2022 Fall 2011 Lecture 4 The Stack int fact int n int res if n 1 return 1 res fact n 1 return n res main 5 arg fact 5 int main int res fact 5 return 0 Memory Model CS 2022 Fall 2011 Lecture 4 The Stack int fact int n int res if n 1 return 1 res fact n 1 return n res main 5 arg 0 fact 5 n 1 int main int res fact 5 return 0 Memory Model CS 2022 Fall 2011 Lecture 4 The Stack int fact int n int res if n 1 return 1 res fact n 1 return n res main 5 arg 0 4 fact 5 n 1 arg int main int res fact 5 return 0 Memory Model CS 2022 Fall 2011 Lecture 4 The Stack int fact int n int res if n 1 return 1 res fact n 1 return n res main 5 arg 0 4 fact 5 n 1 arg fact 4 int main int res fact 5 return 0 Memory Model CS 2022 Fall 2011 Lecture 4 The Stack int fact int n int res if n 1 return 1 res fact n 1 return n res main 5 arg 0 4 0 3 fact 5 n 1 arg fact 4 n 1 arg int main int res fact 5 return 0 Memory Model CS 2022 Fall 2011 Lecture 4 The Stack int fact int n int res if n 1 return 1 res fact n 1 return n res main 5 arg 0 4 0 3 0 2 fact 5 n 1 arg fact 4 n 1 arg int main int res fact 5 return 0 Memory Model fact 3 n 1 arg CS 2022 Fall 2011 Lecture 4 The Stack int fact int n int res if n 1 return 1 res fact n 1 return n res main 5 arg 0 4 0 3 0 2 0 1 fact 5 n 1 arg fact 4 n 1 arg int main int res fact 5 return 0 Memory Model fact 3 n 1 arg fact 2 n 1 arg CS 2022 Fall 2011 Lecture 4 The Stack int fact int n int res if n 1 return 1 res fact n 1 return n res main 5 arg 0 4 0 3 0 2 0 1 fact 5 n 1 arg fact 4 n 1 arg int main int res fact 5 return 0 Memory Model fact 3 n 1 arg fact 2 n 1 arg 1 fact 1 n 1 CS 2022 Fall 2011 Lecture 4 The Stack int fact int n int res if n 1 return 1 res fact n 1 return n res main 5 arg 0 4 0 3 0 2 fact 5 n 1 arg fact 4 n 1 arg int main int res fact 5 return 0 Memory Model fact 3 n 1 arg 1 res 0 fact 2 n 1 CS 2022 Fall 2011 Lecture 4 The Stack int fact int n int res if n 1 return 1 res fact n 1 return n res main 5 arg 0 4 0 3 fact 5 n 1 arg fact 4 n 1 arg 2 int main int res fact 5 return 0 Memory Model res 0 fact 3 n 1 CS 2022 Fall 2011 Lecture 4 The Stack int fact int n int res if n 1 return 1 res fact n 1 return n res main 5 arg 0 4 fact 5 n 1 arg 6 res int main int res fact 5 return 0 Memory Model 0 fact 4 n 1 CS 2022 Fall 2011 Lecture 4 The Stack int fact int n int res if n 1 return 1 res fact n 1 return n res main 5 arg 24 res int main int res fact 5 return 0 Memory Model 0 fact 5 n 1 CS 2022 Fall 2011 Lecture 4 The Stack int fact int n int res if n 1 return 1 res fact n 1 return n res 120 main res int main int res fact 5 return 0 Memory Model CS 2022 Fall 2011 Lecture 4 Stack games main 5 arg I I I Locate the stack Find the direction of stack growth Finding size of stack frame Memory Model 0 4 0 3 fact 5 n 1 arg fact 4 n 1 arg 2 res 0 fact 3 n 1 CS 2022 Fall 2011 Lecture 4 What can go wrong I I Run out of stack space Unintentionally change values on the stack I I I In some other function s frame Even return address from function main 0 4 0 3 fact 5 n 1 arg fact 4 n 1 arg 2 res 0 fact 3 n 1 Access memory even after frame is deallocated Memory Model 5 arg CS 2022 Fall 2011 Lecture 4 Memory Recap I I Program code Function variables I I I I Arguments Local variables Return location Global Variables I I Statically Allocated Dynamically Allocated addr 0xFFFFFFFF Function Stack Frame main return addr fact 5 fact 4 Local Var 2 Local Var 3 Local Var 4 Global Dynamic Heap Global Static Arg 3 next fn Arg 2 next fn Code addr 0x00000000 Memory Model Local Var 1 CS 2022 Fall 2011 Lecture 4 Arg 1 next fn Heap Heap Needed for long term storage that needs to persist across multiple function calls Managed by programmer I I Created by ptr malloc size Destroyed by free ptr MUST check the return value from malloc MUST explicitly free memory when no longer in use Memory Model CS 2022 Fall 2011 Lecture 4 Relevant Library include …
View Full Document
Unlocking...