Memory Model CS 2022: Introduction to C Instructor: Hussam Abu-Libdeh Cornell University (based on slides by Saikat Guha) Fall 2009, Lecture 4 Memory Model CS 2022, Fall 2009, Lecture 4
Memory ModelCS 2022: Introduction to C
Instructor: Hussam Abu-Libdeh
Cornell University(based on slides by Saikat Guha)
Fall 2009, Lecture 4
Memory Model CS 2022, Fall 2009, Lecture 4
Pointers Recap
I int *ptr;
I Pointers are variables that store memoryaddresses of other variables
I Type of variable pointed to depends on type ofpointer:
I int *ptr points to an integer variable,char *ptr points to character variable
I Can cast between pointer types:my int ptr = (int *) my other ptr;
I void *ptr has an unspecified type; must be castto a type before used
Memory Model CS 2022, Fall 2009, Lecture 4
Pointers RecapI Two main operaitons:
I * dereference: get the value at the memorylocation stored in a pointer
I & address of : get the address of a variableI int *my ptr = &my var;
I Pointer arithmetic: directly manipulate apointer’s content to access other locations
I Use with caution!: can access bad areas ofmemory and cause a crash
I However, it is useful in accessing and manipulatingdata structures
I Can have pointers to pointersI int **my 2d array;
Memory Model CS 2022, Fall 2009, Lecture 4
Memory
I Program codeI Function variables
I ArgumentsI Local variablesI Return location
I Global VariablesI Statically AllocatedI Dynamically
Allocated
Memory Model CS 2022, Fall 2009, Lecture 4
The Stack
Stores
I Function local variables
I Temporary variables
I Arguments for next function call
I Where to return when function ends
Memory Model CS 2022, Fall 2009, Lecture 4
The Stack
Managed by compiler
I One stack frame each time function called
I Created when function called
I Stacked on top (under) one another
I Destroyed at function exit
Memory Model CS 2022, Fall 2009, Lecture 4
The Stack
int fact(int n) {int res;if (n == 1)
return 1;res = fact(n-1);return n * res;
}
int main() {int res = fact(5);return 0;
}
Memory Model CS 2022, Fall 2009, Lecture 4
The Stack
int fact(int n) {int res;if (n == 1)
return 1;res = fact(n-1);return n * res;
}
int main() {int res = fact(5);return 0;
}
Memory Model CS 2022, Fall 2009, Lecture 4
The Stack
int fact(int n) {int res;if (n == 1)
return 1;res = fact(n-1);return n * res;
}
int main() {int res = fact(5);return 0;
}
Memory Model CS 2022, Fall 2009, Lecture 4
The Stack
int fact(int n) {int res;if (n == 1)
return 1;res = fact(n-1);return n * res;
}
int main() {int res = fact(5);return 0;
}
Memory Model CS 2022, Fall 2009, Lecture 4
The Stack
int fact(int n) {int res;if (n == 1)
return 1;res = fact(n-1);return n * res;
}
int main() {int res = fact(5);return 0;
}
Memory Model CS 2022, Fall 2009, Lecture 4
The Stack
int fact(int n) {int res;if (n == 1)
return 1;res = fact(n-1);return n * res;
}
int main() {int res = fact(5);return 0;
}
Memory Model CS 2022, Fall 2009, Lecture 4
The Stack
int fact(int n) {int res;if (n == 1)
return 1;res = fact(n-1);return n * res;
}
int main() {int res = fact(5);return 0;
}
Memory Model CS 2022, Fall 2009, Lecture 4
The Stack
int fact(int n) {int res;if (n == 1)
return 1;res = fact(n-1);return n * res;
}
int main() {int res = fact(5);return 0;
}
Memory Model CS 2022, Fall 2009, Lecture 4
The Stack
int fact(int n) {int res;if (n == 1)
return 1;res = fact(n-1);return n * res;
}
int main() {int res = fact(5);return 0;
}
Memory Model CS 2022, Fall 2009, Lecture 4
The Stack
int fact(int n) {int res;if (n == 1)
return 1;res = fact(n-1);return n * res;
}
int main() {int res = fact(5);return 0;
}
Memory Model CS 2022, Fall 2009, Lecture 4
The Stack
int fact(int n) {int res;if (n == 1)
return 1;res = fact(n-1);return n * res;
}
int main() {int res = fact(5);return 0;
}
Memory Model CS 2022, Fall 2009, Lecture 4
The Stack
int fact(int n) {int res;if (n == 1)
return 1;res = fact(n-1);return n * res;
}
int main() {int res = fact(5);return 0;
}
Memory Model CS 2022, Fall 2009, Lecture 4
The Stack
int fact(int n) {int res;if (n == 1)
return 1;res = fact(n-1);return n * res;
}
int main() {int res = fact(5);return 0;
}
Memory Model CS 2022, Fall 2009, Lecture 4
The Stack
int fact(int n) {int res;if (n == 1)
return 1;res = fact(n-1);return n * res;
}
int main() {int res = fact(5);return 0;
}
Memory Model CS 2022, Fall 2009, Lecture 4
The Stack
int fact(int n) {int res;if (n == 1)
return 1;res = fact(n-1);return n * res;
}
int main() {int res = fact(5);return 0;
}
Memory Model CS 2022, Fall 2009, Lecture 4
Stack games
I Locate the stack
I Find the direction ofstack growth
I Finding size of stackframe
Memory Model CS 2022, Fall 2009, Lecture 4
What can go wrong
I Run out of stackspace
I Unintentionallychange values on thestack
I In some otherfunction’s frame
I Even return addressfrom function
I Access memory evenafter frame isdeallocated
Memory Model CS 2022, Fall 2009, Lecture 4
Memory Recap
I Program codeI Function variables
I ArgumentsI Local variablesI Return location
I Global VariablesI Statically AllocatedI Dynamically
Allocated
Memory Model CS 2022, Fall 2009, Lecture 4
Heap
HeapNeeded for long-term storage that needs to persistacross multiple function calls.
Managed by programmer
I Created by ptr = malloc(size)
I 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 2009, Lecture 4
The Heap
int main() {int *p, *q, *r;
p = (int *)malloc(sizeof(int));q = (int *)malloc(
sizeof(int) * 10);r = (int *)malloc(sizeof(int));
if (p == NULL || !q || !r) {... do cleanup ...return 1;
}
free(p);... do other stuff ...
Memory Model CS 2022, Fall 2009, Lecture 4
The Heap
int main() {int *p, *q, *r;
p = (int *)malloc(sizeof(int));q = (int *)malloc(
sizeof(int) * 10);r = (int *)malloc(sizeof(int));
if (p == NULL || !q || !r) {... do cleanup ...return 1;
}
free(p);... do other stuff ...
Memory Model CS 2022, Fall 2009, Lecture 4
The Heap
int main() {int *p, *q, *r;
p = (int *)malloc(sizeof(int));q = (int *)malloc(
sizeof(int) * 10);r = (int *)malloc(sizeof(int));
if (p == NULL || !q || !r) {... do cleanup ...return 1;
}
free(p);... do other stuff ...
Memory Model CS 2022, Fall 2009, Lecture 4
The Heap
int main() {int *p, *q, *r;
p = (int *)malloc(sizeof(int));q = (int *)malloc(
sizeof(int) * 10);r = (int *)malloc(sizeof(int));
if (p == NULL || !q || !r) {... do cleanup ...return 1;
}
free(p);... do other stuff ...
Memory Model CS 2022, Fall 2009, Lecture 4
The Heap
int main() {int *p, *q, *r;
p = (int *)malloc(sizeof(int));q = (int *)malloc(
sizeof(int) * 10);r = (int *)malloc(sizeof(int));
if (p == NULL || !q || !r) {... do cleanup ...return 1;
}
free(p);... do other stuff ...
Memory Model CS 2022, Fall 2009, Lecture 4
Heap games
I Locate the heap
I How freespace ismanaged
I Find how memory isallocated
I How isfragmentationavoided
Memory Model CS 2022, Fall 2009, Lecture 4
What can go wrong
I Run out of heapspace malloc returns 0
I Unintentionallychange other heapdata
I Or clobber heapmetadata
I Access memory afterfree’d
I free memory twice
I Create a memoryleak
Memory Model CS 2022, Fall 2009, Lecture 4