Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s.

Similar presentations


Presentation on theme: "1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s."— Presentation transcript:

1 1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s Material

2 2 A Program’s Address Space Stack 0 N Text Data Code Global variables & string constants (consists of 3 separate sections) Dynamically allocated data Variables with automatic storage duration i.e., local variables and function parameters Variables (data) allocated with malloc Heap PC SP Address of the next instruction to be executed Variables (data) with permanent storage duration, i.e., global and static variables

3 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 here E.g., printf format strings Initializes global & static variables Uninitialized global & static variables Dynamically allocated data Variables with automatic storage duration i.e., local variables and function parameters Variables (data) allocated with malloc Heap PC SP Address of the next instruction to be executed

4 Program Layout – Code Section /* Read-only String */ char *str1 = "String1"; /* Initialized global vars */ char str2[] = "String2 int g1 = 10; /* Uninitialized global vars */ int A[2]; char str3[13]; int g2; /* Main function */ int main(){ int x = 2; /* Local var */ …. } /* end-main */ 0x8048448 main Code Section Increasing Addresses Memory Address

5 Program Layout – Data Section /* Read-only String */ char *str1 = "String1"; /* Initialized global vars */ char str2[] = "String2 int 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 String2 0x8049ba8 g1 10

6 Program Layout – Data Section (cont) /* Read-only String */ char *str1 = "String1"; /* Initialized global vars */ char str2[] = "String2 int g1 = 10; /* Uninitialized global vars */ int A[2]; char str3[13]; int g2; /* Main function */ int main(){ int x = 2; /* Local var */ …. } /* end-main */ 0x8049bb0 Memory Address str2(13) 0x8049bb8 Unused(3) A[0] A[1] 0x8049bb4 g2 0x8049bc8

7 7 Stack: Function Call Frame for “main” Code str1, str2, g1 A[2], str3, g2 x: 2 p: 0x804a008 Text Data main TOS /* Read-only String */ char *str1 = "String1"; /* Initialized global vars */ char str2[] = "String2 int 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 0xbf9dd564 0xbf9dd560 0x804a008 0x8049bb0 0x8048448

8 8 Function Call Frames: When main starts executing #include 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 TOS Heap

9 9 Function Call Frames: Right after main calls factorial #include 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: 5 prod: 1 factorial TOS ToS Heap

10 10 Function Call Frames: After returning from factorial #include 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 main TOS Heap

11 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 main(){ int x = 0; x = Sum(3); printf(“x: %d\n”, x); return 0; } /* end-main */

12 12 Recursive Functions(1) Code x: 0 main ToS Code x: 0 main ToS n: 3 s: 0 Sum(3) When the program starts When Sum(3) is called Code x: 0 main n: 3 s: 0 Sum(3) When Sum(2) is called ToS n: 2 s: 0 Sum(2)

13 13 Recursive Functions(2) Code x: 0 main n: 3 s: 0 Sum(3) When Sum(1) is called n: 2 s: 0 Sum(2) ToS n: 1 s: 0 Sum(1) Code x: 0 main n: 3 s: 0 Sum(3) After Sum(1) returns n: 2 s: 1 Sum(2) ToS

14 14 Recursive Functions(3) Code x: 0 main n: 3 s: 3 Sum(3) After Sum(2) returns ToS Code x: 6 main After Sum(3) returns

15 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: 3 a: 2 Add(2, 3) When Add(2, 3) is called Return Address Base Pointer s: 0


Download ppt "1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s."

Similar presentations


Ads by Google