Understand Execution of a Program

Similar documents
Pointers II. Class 31

Memory and Pointers written by Cathy Saxton

More loops Ch

a data type is Types

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

C++ For Science and Engineering Lecture 15

What Is a Function? Illustration of Program Flow

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

CS201 Some Important Definitions

Lecture Notes on Memory Layout

cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */

Programming Language. Functions. Eng. Anis Nazer First Semester

Functions BCA-105. Few Facts About Functions:

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

G Programming Languages - Fall 2012

6. Pointers, Structs, and Arrays. 1. Juli 2011

7.1 Optional Parameters

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Review of Important Topics in CS1600. Functions Arrays C-strings

1. In C++, reserved words are the same as predefined identifiers. a. True

EL6483: Brief Overview of C Programming Language

In Java we have the keyword null, which is the value of an uninitialized reference type

Intermediate Programming, Spring 2017*

C++ Final Exam 2017/2018

Operators. The Arrow Operator. The sizeof Operator

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Constants, References

ENEE 457: Computer Systems Security. Lecture 16 Buffer Overflow Attacks

Short Notes of CS201

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

CS201 - Introduction to Programming Glossary By

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

C/C++ Functions. Mark Redekopp

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

CS1150 Principles of Computer Science Methods

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

pointers + memory double x; string a; int x; main overhead int y; main overhead

Run Time Environment

Variables, Memory and Pointers

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Agenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips

LAB: INTRODUCTION TO FUNCTIONS IN C++

CS-201 Introduction to Programming with Java

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Unit 6. Thinking with Functions

Computers Programming Course 10. Iulian Năstac

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Compiler Theory. (Semantic Analysis and Run-Time Environments)

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

C++ Programming: From Problem Analysis to Program Design, Third Edition

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

Advanced Pointer & Data Storage

Computer Programming

C++ Programming. Pointers and Memory Management. M1 Math Michail Lampis

MPATE-GE 2618: C Programming for Music Technology. Unit 4.1

CSCI 171 Chapter Outlines

Ch. 11: References & the Copy-Constructor. - continued -

Functions. (transfer of parameters, returned values, recursion, function pointers).

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Run-Time Data Structures

Chapter 13. Recursion. Copyright 2016 Pearson, Inc. All rights reserved.

Pointers, Dynamic Data, and Reference Types

Chapter-13 USER DEFINED FUNCTIONS

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

CS31 Discussion 1E Spring 17 : week 08

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic

2015 Academic Challenge

Functions and Recursion

CS453 CLASSES, VARIABLES, ASSIGNMENTS

6.096 Introduction to C++

Function Call Stack and Activation Records

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

DRAWING ENVIRONMENT DIAGRAMS

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

C++ Programming: Functions

Come and join us at WebLyceum

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

Array Elements as Function Parameters

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Pointers and References

15 FUNCTIONS IN C 15.1 INTRODUCTION

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

Lexical Considerations

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

CS1150 Principles of Computer Science Methods

CPSC 427: Object-Oriented Programming

Wednesday, September 27, 2017

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

CMSC 341 Lecture 2 Dynamic Memory and Pointers

CS2141 Software Development using C/C++ C++ Basics

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Transcription:

Understand Execution of a Program Prof. Zhang September 17, 2014 1 Program in Memory When you execute a program, the program (i.e., executable code) is loaded into the memory (i.e., main memory, RAM) in order to be executed. Below we first review the memory, and then describe the memory map of a program. 1.1 Main Memory The memory of a computer system is a huge array of bytes (each byte is 8 bits, i.e., binary digits). (It s a volatile memory, meaning its content will be lost once the program finishes execution, or if the computer loses power). When declaring or allocating memory space, it is always done in terms of bytes. You can use function sizeof to find out the amount of memory (in the units of bytes) is used for a variable. See the following code segment and comments. double a; int b[20]; cout << "size of a" << sizeof (a) << endl; // this will display 8, as double variable takes up 8 bytes, i.e., 64 bits cout << "size of b" << sizeof (b) << endl; // this will display 80: one int variable is 4 bytes... cout << "size of char type" << sizeof (char) << endl; // this will display 1, each char is represented by 8 bits ASCII code 1.2 Memory of a Program When you run your program (or any program), the program is loaded into the memory (by the operating system). The size of the memory used for a program differs (depending 1

on how large is the executable code, how many variables are used.). The memory a program uses is typically divided into four different areas: the code area, where the compiled program sits in memory. the globals area, where global variables are stored. The heap, where dynamically allocated variables are allocated from. We will learn more about heap later (after we studied pointers). The stack (also called call stack, function call stack. Every time a function is called, a block of memory named call stack frame is pushed to the stack. The call stack frame stores this function call s parameters, local variables, and return address. (The return address keeps track of where to resume execution in the caller function, when this function call returns). When the function returns (finishes its execution), the call stack frame is popped out of the stack (deleted from the stack). The program resumes execution at the statement indicated by the return address. The call stack is kind of like a office space shared by all function calls. Every function, when being called, needs certain memory space to hold its local variables and parameters, and to remember whom to report results to. That memory space is called call stack frame. The main function is the boss (with its office space). When the boss calls upon someone to work on a task, that worker gets his own work space. When someone finishes the task, he cleans up the work space allocated to him; wakes up his boss (who will resumes his work based upon the return address). Example For example: let s draw the snapshot of the memory of the following program, right at the time when func b() is executing its second statement: const int a; void func_b() int i; cout <<"here\n"; void func_a(int a) cout <<"a=" << a << endl; func_b(); 2

cout <<"done\n"; int main() int counter=10; cout <<"calling func_a(); func_a(); 1.3 Runtime errors related to memory Sometimes you will encounter runtime errors that are related to memory usages: Segmentation Fault: if you are accessing memory location that belongs to other program or operating system. This can happen if you access an array element at an index value out of its bounds: Bus Error: if you use an invalid address when accessing memory Stack Overflow: if the program has many levels of function calls, and run out of call stack space to hold the call stack frames for the ongoing calls. 2 Function Call Details 2.1 Funcation call syntax Recall the syntax of calling a function: function_name (list of actual arguments); where the list of actual argument are separated by comma, for example: DisplayArray (NumArray, NumArraySize); The number of actual arguments and their types need to match those in the function s header (declaration and definition). Please note the following two terms we use throughout the class: (Formal) parameters: these are variables and their types as specified in the function declaration and definition (listed in the function s head). 3

(Actual) arguments: there are the expressions, constants, or variables that are supplied in the function call. A Common logic error is to call a function that returns a value, without using the return value:... sum (10,20); pow (2, 8); The above two function calls have no effects: the functions are called and executed, but the returned values are not used. Use return value of a function: if a function returns some value (i.e., return type is not void), then the function call can be used anywhere that value type is expected. For example, if (ContainDuplicate (NumArray, NumArraySize)) // where the function returns bool cout << "Array contains duplicate\n"; else cout << "Array does not contain duplicate\n"; double x = sqrt (10)*34; 3 Tracing function calls: Putting it all together When tracing a program, we follow the execution of a program with a set of sample input values, in order to identify logic errors, understand codes written by others, or to verify your design and code. The keys to program tracing: Remembering where you are using arrows: draw an arrow to point to the current statement in each function that is currently running. 1. Move the arrow following the logic structure (if/else statement, loop statement, and switch statement). 2. When the arrow moves to a function call, draw a new arrow to point to the first statement in the funciton being called. 3. When reaching return statement in main, or end of main s, the program ends. Keeping track of variables 4

We have learnt to use a box, labelled with the name to represent a variable. Every time a value is assigned to the variable, we update the content of the box (so that we remember what value the variable has). In order to trace function calls better, we now keep global variables in the globals area, and keep track of local variables in the call stack. Tracing function call: When we reach a function call: 1. We draw a new call stack frame for the function call on the top of the call stack, and write down the function s local variables, parameters, and return address inside it. 2. Passing parameters: actual arguments are passed into the function, meaning, actual arguments are assigned to formal parameter variables (based upon order). pass-by-value: the values of arguments are assigned to parameters, i.e., the function parameter (which is a local variable) is initialized with the value of the arguments. pass-by-reference: the address of arguments. are assigned to parameters. For tracing purpose, we just write something like, main s counter there. All reference to the parameter are made to the acutal arguments. It s like substituting all occurences of the pass-by-reference parameter by its acutual argument. 3. Write down return address. 4. Pass control: start to execute function body from the first statement in the body 5. Return from function: function s execution ends whenever a return statement is executed, or the end of function body is reached. If a value is returned, the value is returned to the caller. The execution resumed from the next statement in the caller function. Erase (or cross out) the top call stack frame (All local variables of the function are forgotten). 4 Exercise in program tracing 1. Function with both types of parameters void AddTax (double & amt_due, double rate); int main ( ) double total_order = 120.00; double rate = 0.05; 5

cout <<Total order: << total_order <<endl; AddTax (total_order, rate); cout <<With tax total due: << total_order<<endl; cout <<Tax rate is : << rate << endl; void AddTax (double & amt_due, double rate) rate = rate+1.0; amt_due = amt_due*rate; 2. Recursive Function int main() cout << Fibo (3); int Fibo (int n) if (n==1) return 1; else if (n==2) return 1; else int result = Fibo (n-1) + Fibo (n-2); return result; 6