1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function...

Preview:

Citation preview

1

• Program Layout in memory– Code– Data

• Global variables

– Stack• Local variables & function Invocation Frames

– Heap• Dynamically allocated memory

Today’s Material

2

A Program’s Address Space

Stack

0

N

Text

Data

Code

Global variables & string constants

(consists of 3separate sections)

Dynamically allocateddata

Variables with automatic storage durationi.e., local variables and function parameters

Variables (data) allocated with mallocHeap

PC

SP

Address of the next instruction to be executed

Variables (data) with permanent storage duration, i.e., global and static variables

3

A Program’s Address Space (cont)

Stack

0

N

Text

Data

Code

.rodata section

initialized data

uninitialized data (bss)

Readonly string literals would be hereE.g., printf format strings

Initializes global & static variables

Uninitialized global & static variables

Dynamically allocateddata

Variables with automatic storage durationi.e., local variables and function parameters

Variables (data) allocated with mallocHeap

PC

SP

Address of the next instruction to be executed

Program Layout – Code Section/* Read-only String */char *str1 = "String1";

/* Initialized global vars */char str2[] = "String2int g1 = 10;

/* Uninitialized global vars */int A[2]; char str3[13]; int g2;

/* Main function */int main(){ int x = 2; /* Local var */

….} /* end-main */

0x8048448main

Code Section

Incr

easi

ng A

ddre

sses Memory Address

Program Layout – Data Section/* Read-only String */char *str1 = "String1";

/* Initialized global vars */char str2[] = "String2int g1 = 10;

/* Uninitialized global vars */int A[2]; char str3[13]; int g2;

/* Main function */int main(){ int x = 2; /* Local var */

….} /* end-main */

0x80487b4

Readonly Data section

Memory Address

str1

8 bytes

0x8049ba0

Memory Address

str2

8 bytes

Strin

g2

0x8049ba8g1 10

Program Layout – Data Section (cont)

/* Read-only String */char *str1 = "String1";

/* Initialized global vars */char str2[] = "String2int g1 = 10;

/* Uninitialized global vars */int A[2]; char str3[13]; int g2;

/* Main function */int main(){ int x = 2; /* Local var */

….} /* end-main */

0x8049bb0Memory Address

str2(13)

0x8049bb8

Unused(3)

A[0]

A[1]0x8049bb4

g20x8049bc8

7

Stack: Function Call Frame for “main”

Code

str1, str2, g1A[2], str3, g2

x: 2p: 0x804a008

Text

Data

main

TOS

/* Read-only String */char *str1 = "String1";

/* Initialized global vars */char str2[] = "String2int g1 = 10;

/* Uninitialized global vars */int A[2]; char str3[13]; int g2;

/* Main function */int main(){ int x = 2; /* Local var */ char *p = (char *)malloc(16);

….} /* end-main */

Heap

0xbf9dd5640xbf9dd560

0x804a008

0x8049bb0

0x8048448

8

Function Call Frames:When main starts executing

#include <stdio.h>

int factorial(int n);

int main(void){ int fact = 0;

fact = factorial(5);

printf("n! = %d\n", fact);} /* end-main */

int factorial(int n){ int prod = 1;

for( ; n > 1; n--) prod *= n;

return prod;} /* end-factorial */

Code

Data

fact: 0

Text

Data

mainTOS

HeapHeap

9

Function Call Frames:Right after main calls factorial

#include <stdio.h>

int factorial(int n);

int main(void){ int fact = 0;

fact = factorial(5);

printf("n! = %d\n", fact);} /* end-main */

int factorial(int n){ int prod = 1;

for( ; n > 1; n--) prod *= n;

return prod;} /* end-factorial */

Code

Data

fact: 0

Text

Data

main

n: 5prod: 1

factorial

TOS

ToS

HeapHeap

10

Function Call Frames:After returning from factorial

#include <stdio.h>

int factorial(int n);

int main(void){ int fact = 0;

fact = factorial(5);

printf("n! = %d\n", fact);} /* end-main */

int factorial(int n){ int prod = 1;

for( ; n > 1; n--) prod *= n;

return prod;} /* end-factorial */

Code

Data

fact: 120

Text

Data

mainTOS

HeapHeap

11

Recursive Functions: Example

/* Computes 1+2+3+…+n */int Sum(int n){ int s = 0;

/* Base case */ if (n == 1) return 1;

/* Divide and conquer */ s = Sum(n-1);

/* Merge */ return s + n; } /* end-Sum */

#include <stdio.h>

main(){ int x = 0;

x = Sum(3); printf(“x: %d\n”, x);

return 0;} /* end-main */

12

Recursive Functions(1)

Code

x: 0 mainToS

Code

x: 0 main

ToS

n: 3s: 0 Sum(3)

When the program starts

When Sum(3)is called

Code

x: 0 mainn: 3s: 0 Sum(3)

When Sum(2)is called

ToS

n: 2s: 0 Sum(2)

13

Recursive Functions(2)

Code

x: 0 mainn: 3s: 0 Sum(3)

When Sum(1)is called

n: 2s: 0

Sum(2)

ToS

n: 1s: 0

Sum(1)

Code

x: 0 mainn: 3s: 0 Sum(3)

After Sum(1)returns

n: 2s: 1 Sum(2)

ToS

14

Recursive Functions(3)

Code

x: 0 mainn: 3s: 3 Sum(3)

After Sum(2)returns

ToS

Code

x: 6 main

After Sum(3)returns

15

Function Call - Details

int Add(int a, int b){ int s = 0; s = a + b; return s;} /* end-Add */

main(){ int x = 0;

x = Add(2, 3);

return 0;} /* end-main */

Code

x: 0 main

ToS

When the program starts

Code

x: 0 main

ToS

b: 3a: 2

Add(2, 3)

When Add(2, 3)is called

Return Address

Base Pointer

s: 0

Recommended