Computer Programming

Similar documents
Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

Computer Programming

More about BOOLEAN issues

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

ENGI 1020 Introduction to Computer Programming J U L Y 5, R E Z A S H A H I D I

Pointers II. Class 31

Computer Programming

School of Information Technology and Computer Science CSCI192 - Spring Session Revision

DECLARAING AND INITIALIZING POINTERS

Pointer Data Type and Pointer Variables

Data Structures and Algorithms

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Lecture 3 Tao Wang 1

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

Memory and Pointers written by Cathy Saxton

Basic Pointers. CSCI 112: Programming in C

primitive arrays v. vectors (1)

Pointers, Dynamic Data, and Reference Types

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

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

EM108 Software Development for Engineers

Exam 3 Chapters 7 & 9

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

C++ For Science and Engineering Lecture 15

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Introduction to Programming Using Java (98-388)

Topics. bool and string types input/output library functions comments memory allocation templates classes

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

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

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

What is an algorithm?

arrays review arrays and memory arrays: character array example cis15 advanced programming techniques, using c++ summer 2008 lecture # V.

CAAM 420 FALL 2012 Lecture 9. Mishael Owoyemi

Quiz 0 Review Session. October 13th, 2014

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Launchpad Lecture -10

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

C++ Programming Chapter 7 Pointers

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

cout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];

Homework #3 CS2255 Fall 2012

CS349/SE382 A1 C Programming Tutorial

Initializing and Finalizing Objects

Lecture 04 Introduction to pointers

Computer Programming

C++ Final Exam 2017/2018

C++ basics Getting started with, and Data Types.

CS113: Lecture 4. Topics: Functions. Function Activation Records

CS240: Programming in C

CSCE 110 PROGRAMMING FUNDAMENTALS

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

M.CS201 Programming language

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

Computer Programming. Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

Computer Programming Lecture 12 Pointers

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

Dynamic Memory Allocation

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

The C++ Language. Arizona State University 1

Homework Assignment #1

Topics: Material through example 19 (types, operators, expressions, functions, selection, loops, arrays)

Object oriented programming C++

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst


CS101 Computer programming and utilization

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

[0569] p 0318 garbage

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

Introduction to Computer Science Midterm 3 Fall, Points

Lab Session # 7 Pointers. ALQUDS University Department of Computer Engineering

BITG 1233: Introduction to C++

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Using Methods. Writing your own methods. Dr. Siobhán Drohan Mairead Meagher. Produced by: Department of Computing and Mathematics

1.00 Lecture 4. Promotion

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Transcription:

Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Introduction to Pointers Part 1 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1

Quick Recap of Relevant Topics Basic programming constructs Variables and basic data types int, float, double, char, bool, void Arrays and matrices Programs to solve some interesting problems Variables: Named memory locations Memory locations accessed through names 2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Overview of This Lecture Addresses of memory locations Address of operator in C++ Pointer data type in C++ Motivate accessing memory locations through addresses 3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty,

Memory and Addresses Main memory is a sequence of physical storage locations Each location stores 1 byte (8 bits): Content/value of location Each physical memory location identified by a unique address Index in sequence of memory locations Address (in hexadecimal) 400 401 402 403 404 405 406 407 408 409 40a 1 0 0 1 1 1 0 1 1 0 1 1 1 1 1 1 1 0 0 1 0 0 0 1 1 0 1 1 0 1 1 1 1 0 0 1 0 0 0 1 1 0 0 0 0 1 1 1 1 1 1 1 0 0 0 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 4

Memory For Executing A Program (Process) Operating system allocates a part of main memory for use by a process Divided into: Code segment: Stores executable instructions in program Data segment: For dynamically allocated data (later lecture) Stack segment: Call stack CODE SEGMENT Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 5

Program Variables and Memory Named drawers of Dumbo Named locations in stack segment // Rest of code What are their addresses in memory? Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 6

Memory and Program Variables 4 bytes for a in stack segment // Rest of code Each addressable memory location stores 1 byte CODE SEGMENT Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 7

Memory and Program Variables // Rest of code CODE SEGMENT Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 8

Memory and Program Variables // Rest of code 4 bytes for b in stack segment CODE SEGMENT Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 9

Memory and Program Variables // Rest of code 1 byte for c in stack segment CODE SEGMENT Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 10

Memory and Program Variables // Rest of code Memory Address 0x00104 0x00200-0x00203 0x00402-0x00405 CODE SEGMENT Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 11

Memory and Program Variables cin >> a; cin >> c; b = (a + c)/2.0; return 0; Memory Address 0x00104 0x00200-0x00203 0x00402-0x00405 Accessing memory locations by names CODE SEGMENT Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 12

Memory and Program Variables cin >> a; cin??? >> b; c = (a + b)/2; Memory Address 0x00104 0x00200-0x00203 0x00402-0x00405 Can we access memory locations by their addresses? CODE SEGMENT Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 13

Memory and Program Variables cin >> a; cin??? >> b; c = (a + b)/2; Memory Address 0x00104 0x00200-0x00203 0x00402-0x00405 YES (this lecture and next)!!! If we can find the addresses CODE SEGMENT of memory locations corresponding to variables Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 14

How Do We Get an Address? C++ provides an address of operator: unary & If a is a program variable, &a gives address of a in memory Unary operator: Takes a single argument &a is a C++ expression Worry about operator precedence, associativity??? Simplify life: use parentheses Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 15

Pointer as a Type If a is an int variable, what is the type of &a Pointer to int, written in C++ as the type int * If b is a float variable, type of &b is pointer to float, written as float * In general, if x is a variable of type T T * is the type pointer to T &x is an expression of type T *, gives address of x If int * is a type, can t we have a variable of type int *? Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 16

Pointer Variables in a C++ Program int * ptra; // Rest of code Memory Address 0x00104 0x00200-0x00203 0x00402-0x00405 0x0040a - 0x0040d CODE SEGMENT Assume, all memory addresses are 32 bits (4 bytes) Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 17

Pointer Variables in a C++ Program int * ptra; ptra = &a; // Rest of code Memory Address 0x00104 0x00200-0x00203 0x00402-0x00405 0x0040a - 0x0040d CODE SEGMENT Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 18

Pointer Variables in a C++ Program int * ptra; ptra = &a; // Rest of code Memory Address 0x00104 0x00200-0x00203 0x00402-0x00405 0x0040a - 0x0040d CODE SEGMENT What is the value assigned to ptra? 0x00402, 0x00403, 0x00404 or 0x00405? Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 19

Pointer Variables in a C++ Program int * ptra; ptra = &a; // Rest of code Memory Address 0x00104 0x00200-0x00203 0x00402-0x00405 0x0040a - 0x0040d 0x00402 CODE SEGMENT What is the value assigned to ptra? 0x00402: Address of first among bytes allocated for a Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 20

Value and Address of Pointer Variables int * ptra; ptra = &a; // Rest of code Memory Address 0x00104 0x00200-0x00203 0x00402-0x00405 0x0040a - 0x0040d 0x00402 CODE SEGMENT Content/Value of ptra: 0x00402 Address of ptra: 0x0040a Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 21

Can We Have Pointers to Pointers? ptra is a variable of type int * (pointer to int) What is the type of the expression &ptra? Recall: If variable x is of type T, &x is of type T * Type of &ptra is int ** : Pointer to pointer to int Note: We don t write (int *) *, but int ** How far can we take this? As far as we want int **** is a legitimate pointer type in C++ pointer to pointer to pointer to pointer to int Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 22

A Note About Pointer Declarations int x, y, z; int *a, b, *c; // Rest of code Types of x, y and z: int Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 23

A Note About Pointer Declarations int x, y, z; int *a, b, *c; // Rest of code Type of a: pointer to int Type of b: int, not pointer to int Type of c: pointer to int, not pointer to pointer to int Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 24

A C++ Program For Printing Addresses int * ptra; float * ptrb; char * ptrc; ptra = &a; cout << Address of a is: << ptra; ptrb = &b; cout << Address of b is: << ptrb; ptrc = &c; cout << Address of c is: << ptrc; return 0; Compile and run this program See how memory addresses look like!!! Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 25

A C++ Program For Printing Addresses int * ptra; float * ptrb; char * ptrc; ptra = &a; cout << Address of a is: << ptra; ptrb = &b; cout << Address of b is: << ptrb; ptrc = &c; cout << Address of c is: << ptrc; return 0; What do we do with these addresses? Wait for a few more lectures!!! Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 26

Summary Memory and addresses Address of operator in C++ Pointer data type in C++ Some simple usage in programs Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 27