Top Banner
Memory Model CS 113: Introduction to C Instructor: Saikat Guha Cornell University Spring 2007, Lecture 4 Memory Model CS 113, Spring 2007, Lecture 4
30

Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

Jun 24, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

Memory ModelCS 113: Introduction to C

Instructor: Saikat Guha

Cornell University

Spring 2007, Lecture 4

Memory Model CS 113, Spring 2007, Lecture 4

Page 2: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

Memory

◮ Program code◮ Function variables

◮ Arguments◮ Local variables◮ Return location

◮ Global Variables◮ Statically Allocated◮ Dynamically

Allocated

addr=0xFFFFFFFF

addr=0x00000000

Code

Global Static

GlobalDynamic(Heap)

fact(4)

fact(5)

main()

FunctionStack Frame

return addr

Local Var 1

Local Var 2

Local Var 3

Local Var 4

Arg 3 next fn

Arg 2 next fn

Arg 1 next fn

Memory Model CS 113, Spring 2007, Lecture 4

Page 3: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

The Stack

Stores

◮ Function local variables

◮ Temporary variables

◮ Arguments for next function call

◮ Where to return when function ends

Memory Model CS 113, Spring 2007, Lecture 4

Page 4: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

The Stack

Managed by compiler

◮ One stack frame each time function called

◮ Created when function called

◮ Stacked on top (under) one another

◮ Destroyed at function exit

Memory Model CS 113, Spring 2007, Lecture 4

Page 5: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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;

}

main()

Memory Model CS 113, Spring 2007, Lecture 4

Page 6: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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;

}

main()arg

5

Memory Model CS 113, Spring 2007, Lecture 4

Page 7: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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;

}

main()arg

5

fact(5)

Memory Model CS 113, Spring 2007, Lecture 4

Page 8: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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;

}

main()arg

5

fact(5) 0

n==1

Memory Model CS 113, Spring 2007, Lecture 4

Page 9: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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;

}

main()arg

5

fact(5) 0

n==1 arg

4

Memory Model CS 113, Spring 2007, Lecture 4

Page 10: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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;

}

main()arg

5

fact(5) 0

n==1 arg

4

fact(4)

Memory Model CS 113, Spring 2007, Lecture 4

Page 11: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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;

}

main()arg

5

fact(5) 0

n==1 arg

4

fact(4) 0

n==1 arg

3

Memory Model CS 113, Spring 2007, Lecture 4

Page 12: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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;

}

main()arg

5

fact(5) 0

n==1 arg

4

fact(4) 0

n==1 arg

3

fact(3) 0

n==1 arg

2

Memory Model CS 113, Spring 2007, Lecture 4

Page 13: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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;

}

main()arg

5

fact(5) 0

n==1 arg

4

fact(4) 0

n==1 arg

3

fact(3) 0

n==1 arg

2

fact(2) 0

n==1 arg

1

Memory Model CS 113, Spring 2007, Lecture 4

Page 14: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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;

}

main()arg

5

fact(5) 0

n==1 arg

4

fact(4) 0

n==1 arg

3

fact(3) 0

n==1 arg

2

fact(2) 0

n==1 arg

1

fact(1) 1

n==1

Memory Model CS 113, Spring 2007, Lecture 4

Page 15: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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;

}

main()arg

5

fact(5) 0

n==1 arg

4

fact(4) 0

n==1 arg

3

fact(3) 0

n==1 arg

2

fact(2) 0

n==1

1

res

Memory Model CS 113, Spring 2007, Lecture 4

Page 16: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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;

}

main()arg

5

fact(5) 0

n==1 arg

4

fact(4) 0

n==1 arg

3

fact(3) 0

n==1

2

res

Memory Model CS 113, Spring 2007, Lecture 4

Page 17: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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;

}

main()arg

5

fact(5) 0

n==1 arg

4

fact(4) 0

n==1

6

res

Memory Model CS 113, Spring 2007, Lecture 4

Page 18: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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;

}

main()arg

5

fact(5) 0

n==1

24

res

Memory Model CS 113, Spring 2007, Lecture 4

Page 19: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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;

}

main()120

res

Memory Model CS 113, Spring 2007, Lecture 4

Page 20: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

Stack games

◮ Locate the stack

◮ Find the direction ofstack growth

◮ Finding size of stackframe

main()arg

5

fact(5) 0

n==1 arg

4

fact(4) 0

n==1 arg

3

fact(3) 0

n==1

2

res

Memory Model CS 113, Spring 2007, Lecture 4

Page 21: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

What can go wrong

◮ Run out of stackspace

◮ Unintentionallychange values on thestack

◮ In some otherfunction’s frame

◮ Even return addressfrom function

◮ Access memory evenafter frame isdeallocated

main()arg

5

fact(5) 0

n==1 arg

4

fact(4) 0

n==1 arg

3

fact(3) 0

n==1

2

res

Memory Model CS 113, Spring 2007, Lecture 4

Page 22: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

Memory Recap

◮ Program code◮ Function variables

◮ Arguments◮ Local variables◮ Return location

◮ Global Variables◮ Statically Allocated◮ Dynamically

Allocated

addr=0xFFFFFFFF

addr=0x00000000

Code

Global Static

GlobalDynamic(Heap)

fact(4)

fact(5)

main()

FunctionStack Frame

return addr

Local Var 1

Local Var 2

Local Var 3

Local Var 4

Arg 3 next fn

Arg 2 next fn

Arg 1 next fn

Memory Model CS 113, Spring 2007, Lecture 4

Page 23: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

Heap

Heap

Needed for long-term storage that needs to persistacross multiple function calls.

Managed by programmer

◮ 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 113, Spring 2007, Lecture 4

Page 24: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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 113, Spring 2007, Lecture 4

Page 25: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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 ...

4 bytesMetadata

Memory Model CS 113, Spring 2007, Lecture 4

Page 26: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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 ...

4 bytesMetadata

40 bytes

Metadata

Memory Model CS 113, Spring 2007, Lecture 4

Page 27: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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 ...

4 bytesMetadata

40 bytes

Metadata

4 bytesMetadata

Memory Model CS 113, Spring 2007, Lecture 4

Page 28: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

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 ...

40 bytes

Metadata

4 bytesMetadata

Memory Model CS 113, Spring 2007, Lecture 4

Page 29: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

Heap games

◮ Locate the heap

◮ How freespace ismanaged

◮ Find how memory isallocated

◮ How isfragmentationavoided

4 bytesMetadata

40 bytes

Metadata

Memory Model CS 113, Spring 2007, Lecture 4

Page 30: Memory Model - CS 113: Introduction to C · Memory Program code Function variables Arguments Local variables Return location Global Variables Statically Allocated Dynamically Allocated

What can go wrong

◮ Run out of heapspace malloc returns 0

◮ Unintentionallychange other heapdata

◮ Or clobber heapmetadata

◮ Access memory evenfree’d

◮ free memory twice

◮ Create a memoryleak

4 bytesMetadata

40 bytes

Metadata

Memory Model CS 113, Spring 2007, Lecture 4