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

Slides:



Advertisements
Similar presentations
1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Advertisements

Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
The University of Adelaide, School of Computer Science
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
SPARC Architecture & Assembly Language
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
The University of Adelaide, School of Computer Science
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
Informática II Prof. Dr. Gustavo Patiño MJ
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
COMP3221: Microprocessors and Embedded Systems Lecture 12: Functions I Lecturer: Hui Wu Session 2, 2005.
David Notkin Autumn 2009 CSE303 Lecture 10 "If it weren't for C, we'd be writing programs in BASI, PASAL, and OBOL."
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
CS 536 Spring Run-time organization Lecture 19.
Slide 1 Chapter 5-b Attributes of variables. Slide 2 Attributes of variables { int sum = 5;... } sum int IdentifierType Value 0110.
Run-Time Storage Organization
Intro to Computer Architecture
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
C and Data Structures Baojian Hua
Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.
Run-time Environment and Program Organization
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Memory Layout C and Data Structures Baojian Hua
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
Outline Midterm results Static variables Memory model
The Stack Pointer and the Frame Pointer (Lecture #19) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
Program Compilation and Execution. Today’s Objectives Explain why runtime stack needed for C Explain why runtime stack needed for C Draw logical division.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Smashing the Stack Overview The Stack Region Buffer Overflow
Part I The Basic Idea software sequence of instructions in memory logically divided in functions that call each other – function ‘IE’ calls function.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function.
Intermediate Representation II Storage Allocation and Management EECS 483 – Lecture 18 University of Michigan Wednesday, November 8, 2006.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
Today’s Material Strings Definition Representation Initialization
Int main( ) { x = a(); } int a() { y = b(); } int b() { z = c(); } int c() { } 1.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
Chapter 14 Functions.
Run-Time Environments Chapter 7
Pointers and dynamic memory
Run-time organization
Procedures (Functions)
CSC 253 Lecture 8.
Lecture 4: Process Memory Layout
CSC 253 Lecture 8.
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
Memory Allocation CS 217.
The University of Adelaide, School of Computer Science
Understanding Program Address Space
Pointers and dynamic memory
C (and C++) Pointers April 4, 2019.
Computer Architecture and System Programming Laboratory
Computer Architecture
Pointers, Dynamic Data, and Reference Types
Run-time environments
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

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

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 */ 0x main Code Section Increasing Addresses Memory Address

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

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 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 0x

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 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 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 Recursive Functions: Example /* Computes …+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 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 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 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 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