memory model - cs 2022: introduction to c · i int *my ptr = &my var; i pointer arithmetic:...

32
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

Upload: others

Post on 16-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 2: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 3: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 4: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 5: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 6: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 7: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 8: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 9: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 10: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 11: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 12: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 13: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 14: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 15: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 16: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 17: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 18: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 19: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 20: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 21: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 22: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 23: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 24: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 25: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 26: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 27: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 28: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 29: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 30: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 31: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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

Page 32: Memory Model - CS 2022: Introduction to C · I int *my ptr = &my var; I Pointer arithmetic: directly manipulate a pointer’s content to access other locations I Use with caution!:

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