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

Similar documents
today cs3157-fall2002-sklar-lect05 1

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Homework #3 CS2255 Fall 2012

What is an algorithm?

Array Elements as Function Parameters

DECLARAING AND INITIALIZING POINTERS

Pointers, Dynamic Data, and Reference Types

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

Object Reference and Memory Allocation. Questions:

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

aggregate data types member functions cis15 advanced programming techniques, using c++ lecture # II.1

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

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

Computer Programming

Pointers as Arguments

a data type is Types


calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

CS201- Introduction to Programming Current Quizzes

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

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

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

C++ for Java Programmers

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

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

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Object-Oriented Principles and Practice / C++

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

Exam 3 Chapters 7 & 9

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

Object oriented programming C++

Principles of C and Memory Management

BITG 1113: POINTER LECTURE 12

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

C programming Lecture 2. Marina Krstic Marinkovic School of Mathematics Trinity College Dublin

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

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

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

Goals of this Lecture

Fundamentals of Programming Session 20

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

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

First of all, it is a variable, just like other variables you studied

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

W3101: Programming Languages C++ Ramana Isukapalli

Scope. Scope is such an important thing that we ll review what we know about scope now:

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

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

CS102: Variables and Expressions

Lecture 23: Pointer Arithmetic

Operator overloading

MYcsvtu Notes LECTURE 34. POINTERS

CS102 Software Engineering Principles

Introduction to Computer Science Midterm 3 Fall, Points

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Objectives. In this chapter, you will:

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Introduction to C: Pointers

Functions and Recursion

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Procedural Programming & Fundamentals of Programming

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

More about BOOLEAN issues

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Data Representation and Storage

Computer Systems Lecture 9

The University of Nottingham

C++ Programming Chapter 7 Pointers

Functions. Introduction :

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Functions, Pointers, and the Basics of C++ Classes

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

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

CS201 Some Important Definitions

Introduction to C++ Systems Programming

Lecture 10: Introduction to Inheritance

Data Representation and Storage. Some definitions (in C)

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

Dynamic Memory Allocation

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

Introduction to Scientific Computing and Problem Solving

CPSC 427: Object-Oriented Programming

UNIT- 3 Introduction to C++

OBJECT ORIENTED PROGRAMMING

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Memory, Arrays, and Parameters

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

Pointers and Arrays 1

Transcription:

topics: arrays pointers arrays of objects resources: cis15 advanced programming techniques, using c++ summer 2008 lecture # V.1 some of this lecture is covered in parts of Pohl, chapter 3 arrays review a data structure consisting of related elements of the same data type an array is a regular grouping or ordering a string is an array of characters an array has a length associated with it arrays need: data type name length length can be determined: statically at compile time e.g., char str1[10]; dynamically at run time e.g., char *str2; (we ll talk about how to do this in our next lecture) cis15-summer2008-ozgelen-lecv.1 1 cis15-summer2008-ozgelen-lecv.1 2 arrays and memory arrays: character array example defining a variable is called allocating memory to store that variable defining an array means allocating memory for a group of bytes, i.e., assigning a label to the first byte in the group individual array elements are indexed starting with 0 ending with length 1 indeces follow array name, enclosed in square brackets ([ ]) e.g., arr[25] // example: arrays0c.cpp const int MAX = 6; int main( void ) { char str[max] = "ABCDE"; int i; for ( i=0; i<max-1; i++ ) { cout << str[i] << " "; /* end of main() */ cis15-summer2008-ozgelen-lecv.1 3 cis15-summer2008-ozgelen-lecv.1 4

arrays: integer array example pointers overview // example: arrays0i.cpp const int MAX = 6; int arr[max] = { -45, 6, 0, 72, 1543, 62 ; int i; for ( i=0; i<max; i++ ) { cout << arr[i] << " "; /* end of main() */ a pointer contains the address of an element allows one to access the element indirectly & = unary operator that gives address of its argument * = unary operator that fetches contents of its argument (i.e., its argument is an address) note that & and * bind more tightly than arithmetic operators you can print the value of a pointer using cout with the pointer or using C-style printing (e.g., printf()) and the formatting character %p cis15-summer2008-ozgelen-lecv.1 5 cis15-summer2008-ozgelen-lecv.1 6 pointers: memory addresses (1) pointers: memory addresses (2) declaring pointers: pointers indirectly address memory where data of the types we ve already discussed is stored (e.g., int, char, float, etc. even classes) declaration uses asterisks (*) to indicate a pointer to a memory location storing a particular data type int *count; float *avg; ampersand & is used to get the address of a variable int count = 12; int *countptr = &count; &count returns the address of count and stores it in the pointer variable countptr a picture: countptr count 12 cis15-summer2008-ozgelen-lecv.1 7 cis15-summer2008-ozgelen-lecv.1 8

pointers: memory addresses (3) pointers: address arithmetic here s another example: int i = 3, j = -99; int count = 12; int *countptr = &count; and here s what the memory looks like: variable name memory location value count 0xbffff4f0 12 i 0xbffff4f4 3 j 0xbffff4f8-99... countptr 0xbffff600 0xbffff4f0... an array is some number of contiguous memory locations an array definition is really a pointer to the starting memory location of the array and pointers are really integers so you can perform integer arithmetic on them e.g., +1 increments a pointer, -1 decrements you can use this to move from one array element to another cis15-summer2008-ozgelen-lecv.1 9 cis15-summer2008-ozgelen-lecv.1 10 // pointers0.cpp int i, *j, arr[5]; for ( i=0; i<5; i++ ) { arr[i] = i; pointers: example cout << "i=" << i; cout << " arr[i]=" << arr[i]; cout << " &arr[i]=" << &arr[i]; j = &arr[0]; cout << "j=" << j; cout << " *j=" << *j; cout << endl << endl;; j++; cout << "after adding 1 to j: j=" << j; cout << " *j=" << *j << endl; cout << "arr=" << arr << endl; for ( i=0; i<5; i++ ) { cis15-summer2008-ozgelen-lecv.1 11 cis15-summer2008-ozgelen-lecv.1 12

and the output is... arr=0xbffff864 i=0 arr[i]=0 &arr[i]=0xbffff864 i=1 arr[i]=1 &arr[i]=0xbffff868 i=2 arr[i]=2 &arr[i]=0xbffff86c i=3 arr[i]=3 &arr[i]=0xbffff870 i=4 arr[i]=4 &arr[i]=0xbffff874 j=0xbffff864 *j=0 after adding 1 to j: j=0xbffff868 *j=1 NOTE that the absolute pointer values can change each time you run the program! BUT the relative values will stay the same. // pointers1.cpp int x, y; int *; x = 3; pointers: another example // declare two ints // declare a pointer to an int // initialize x = &x; // set to the value of the address of x; // i.e., to point to x y = *; // set y to the value stored at the address pointed // to by ; in other words, the value of x cis15-summer2008-ozgelen-lecv.1 13 cis15-summer2008-ozgelen-lecv.1 14 // x=3 =0xbffffce0 y=3 // note that the precise value of will depend on the machine // and may change each time the program is run, because its value // depends on what portion of memory is allocated to the program // by the operating system at the time that the program is run x++; // increment x // x=4 =0xbffffce0 y=3 // note that the value of x changes, but not or y (*)++; // increment the value stored at the address pointed // to by cis15-summer2008-ozgelen-lecv.1 15 // x=5 =0xbffffce0 y=3 // note that the value of x changes, because contains the // address of x // what happens if we take away the parens? *++; // x=5 =0xbffffce4 y=3 // the value of changes -- is that what you expected? // also note that it goes up by 4 -- because it is an integer pointer // and integers take up 4 bytes // since has changed, what does it point to now? printf( "*=%d\n",* ); // the output is: // *=3 // because now points to y s address -- this is because y was cis15-summer2008-ozgelen-lecv.1 16

// declared right after x was declared. note that this is usually // the case, but not necessarily. use an array to ensure contiguity of // addresses. and the output is... step 0: here is what we start with: x=3 =0xbffff874 y=3 step 1: after incrementing x: x=4 =0xbffff874 y=3 step 2: after incrementing (*): x=5 =0xbffff874 y=3 step 3: after incrementing *: x=5 =0xbffff878 y=3 and *=3 cis15-summer2008-ozgelen-lecv.1 17 cis15-summer2008-ozgelen-lecv.1 18 and here s a picture of what s going on: pointers and references step 0: x = 3 y = 3 this is the initial situation: x is initialized to the value 3 is initialized to point to x y is initialized to the value pointed to by x pointers (same as in C): int *p means pointer to int p = &i means p gets the address of object i step 1: here is the situation after incrementing x x = 4 y = 3 references (not in C): they are basically aliases alternative names for the values stored at the indicated memory locations, e.g.: step 2: x = 5 y = 3 here is the situation after incrementing (*), i.e., the value that points to, in other words, x int n; int &nn = n; double arr[10]; double &last = arr[9]; the difference between them: step 3: x = 5 y = 3 here is the situation after incrementing i.e., the POINTER increments, in other words, it moves to point to the next contiguous item in memory, in this case, y // refs.cpp #include <cstdio> cis15-summer2008-ozgelen-lecv.1 19 cis15-summer2008-ozgelen-lecv.1 20

int A = 1; // declare and define A int B = 2; // declare and define next memory location after A int *p = &A; // p points to A int &refa = A; // alias (reference) for A *p = 3; // *p points to A, so A is assigned 3 cout << "initially: "; A = *p + 1; // A is assigned value of *p=3 plus 1 cout << "after A=*p+1: "; A++; cout << "after A++ "; p++; cout << "after p++ "; refa++; cout << "after refa++ "; A = refa + 1; // A is assigned value of refa, now 4, plus 1 cout << "after A=refA+1: "; cis15-summer2008-ozgelen-lecv.1 21 cis15-summer2008-ozgelen-lecv.1 22 and the output is: initially: A=3 p=0xbffff864 *p=3 refa=3 &A=0xbffff864 &p=0xbffff860 &refa=0xbffff864 after A=*p+1: A=4 p=0xbffff864 *p=4 refa=4 &A=0xbffff864 &p=0xbffff860 &refa=0xbffff864 after A=refA+1: A=5 p=0xbffff864 *p=5 refa=5 &A=0xbffff864 &p=0xbffff860 &refa=0xbffff864 after A++ A=6 p=0xbffff864 *p=6 refa=6 &A=0xbffff864 &p=0xbffff860 &refa=0xbffff864 after p++ A=6 p=0xbffff868 *p=2 refa=6 &A=0xbffff864 &p=0xbffff860 &refa=0xbffff864 after refa++ A=7 p=0xbffff868 *p=2 refa=7 &A=0xbffff864 &p=0xbffff860 &refa=0xbffff864 arrays of objects you can create arrays of objects just as you create arrays of primitive data types /* arrayso.cpp */ class Point { private: int x, y; public: Point() { Point( int x0, int y0 ) : x(x0), y(y0) { void set( int x0, int y0 ) { x = x0; y = y0; int getx() { return x; int gety() { return y; cis15-summer2008-ozgelen-lecv.1 23 cis15-summer2008-ozgelen-lecv.1 24

void print() const { cout << "(" << x << "," << y << ") "; ; Point triangle[3]; triangle[0].set( 0,0 ); triangle[1].set( 0,3 ); triangle[2].set( 3,0 ); cout << "here is the triangle: "; for ( int i=0; i<3; i++ ) { triangle[i].print(); pointers to objects you can also create pointers to objects just as you create pointers to primitive data types in the example below, we demonstrate dynamic memory allocation by declaring a pointer to an array and then LATER declaring the memory for the array using the new function at the end of the program, we call the delete function to de-allocate the memory (it s not really necessary at the end of a program, but you might want to use it inside a program to keep your memory management clean) we ll talk more about dynamic memory allocation and memory management in the next lecture... /* arrayso1.cpp */ class Point { cis15-summer2008-ozgelen-lecv.1 25 cis15-summer2008-ozgelen-lecv.1 26 private: int x, y; public: Point() { Point( int x0, int y0 ) : x(x0), y(y0) { void set( int x0, int y0 ) { x = x0; y = y0; int getx() { return x; int gety() { return y; void print() const { cout << "(" << x << "," << y << ") "; ; delete[] triagain; Point *triagain = new Point[3]; assert( triagain!= 0 ); triagain[0].set( 0,0 ); triagain[1].set( 0,3 ); triagain[2].set( 3,0 ); cout << "tri-ing again: "; for ( int i=0; i<3; i++ ) { triagain[i].print(); cis15-summer2008-ozgelen-lecv.1 27 cis15-summer2008-ozgelen-lecv.1 28