Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007.

Post on 20-Jan-2016

222 views 4 download

Tags:

Transcript of Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007.

Chapter 1Arrays, Pointers, and Structures

Saurav KarmakarSpring 2007

Syntax vs. Semantics

Syntax: the form of the presentation

Semantics: the meaning of the

presention

1.1 What are Arrays, Pointers, and Structures

Pointers: store an address. Used to access objects

Aggregate: collection of objects stored in one unit.

Arrays: indexed collections of identical-type objects. (aggregate)

Structures: collections of objects that need not be of the same type. (aggregate)

1.2 Arrays and Strings

Arrays: indexed collections of identical-type objects.

Array index always start on 0 Arrays can be used in two different

ways: primitive arrays and vectors. First class object (vectors) vs.

second class object (arrays)

First class object (vectors) vs. second class object (arrays)

First class object (vectors, string class) : can be manipulate in all the usual ways.

second class object (arrays) : can be manipulate in only certain restricted ways.

Vectors

#include <vector> Declaration:

vector<type>identifiers(size); Example: vector<int> a(3);

Vector can be indexed as an array, using [ ]

VectorsVector member functions:

vector<int> v(10);

v.at() // equal to v[ ] v.size() // return the number of elements in v v.front() // return the first element in v v.back() // return the last element in v v.clear() // delete all the element in v v.empty() // return 1 if v is empty; else return

0

Vectors v.resize( val ) // change size of v to val v.pop_back() // remove the last element in v v.push_back( val ) // appends val to the end of v v.capacity() // return the number of element that vector can hold before it will need to

allocate more space

http://www.cppreference.com/cppvector/all.html

How to do Vector’s resize

Example:

vector<int> arr(10); arr.resize(12);

Function calls Call by value: The actual argument

is copied into the parameter ex: int findMax(vector<int> a);

Call by reference: avoids copy, it allows change of the parameter

int findMax(vector<int> &a);

Function calls

Call by constant reference: avoids copy and guarantees that actual parameter will not be change

ex. int findMax(const vector<int> &a);

Multidimensional Array

Second class object

First class equivalent is matrix

Syntax : matrix<int> x(2,3);

Strings #include<string> string s=“Hello World!”; Int size=s.length(); cout<< s[4] <<endl; //

result:“o” cout<< s <<endl;

1.3 Pointers syntax in C++ How to Declare a pointer int *ptr; & : unary operator that returns the

address of the object, it is placed before. int x=5; int *ptr; ptr=&x; cout << ptr << endl; // output:

0013FF7C

Pointer cont. * : unary deferencing operator which

can access data/object being pointed. *ptr = 10;Example: int x=5; int *ptr=&x; //*ptr=5 ptr= 0013FF7C *ptr=10 //*ptr=x=10

Illegal: ptr=x //x is not an address

Legal Pointer Syntax

int x=10Declare a pointer: int *ptr=&x or int *ptr ptr=&x After declare: *ptr=15

Illegal Pointer Syntax

int *ptr //run time error

*ptr=&x

ptr=x or int *ptr=x

Pointer

*ptr = x // Symantically incorrect

What happens bellow ? int x=5; int *ptr = &x; *ptr +=1; *ptr++;

Pointers

What happens bellow ? type *ptr1, *ptr2; ptr1=ptr2; *ptr1=*ptr2;

C++ is strongly typed language.

1.4 Dynamic memory management

new operator allocates memory and returns a pointer to the newly created object.

When an object, that is allocated by new, is no longer referenced, an operator delete must be applied to the object, through its pointer, to avoid “memory leakage”

Cont. Example: string *str_ptr; str_ptr = new string("Hello"); cout<< *str_ptr << endl; //Hellow

cout<< (*str_ptr).length() <<endl; //5

delete str_ptr;

Stale pointer… double delete

string *s = new string(“hello”);

string *t=s;

delete t;

1.5 Reference variables Reference type is an alias and may

be viewed as a pointer constant that is always dereferenced implicitly.

Reference variables must be initialized at declaration time.

Viz. int l=0;int &c = l;

c = s; // Not allowed

Cont.#include<iostream>using namespace std;

void swap_wrong (int a, int b){int temp=a; a=b; b=temp;}

void swap_c (int *a, int *b){int temp=*a; *a=*b; *b=temp;}

void swap_ref (int &a, int &b){int temp=a; a=b; b=temp;}

void main(){

int x=5,y=7;swap_wrong(x,y); // x=5, y=7swap_c(&x,&y); // x=7, y=5swap_ref(x,y); // x=5, y=7

}

Structures

Structures: collections of objects that need not be of the same type.

Syntax: struct identifier { };

Structures

Struct Student{

string firstName; string lastName; …}; Student a, *sPtr; // declaring Student a.firstName= “Mary”;//modifying member data sPtr= &a; (*sPtr).lastName= “Smith”; sPtr->lastName= “Smith”; // same as above

Structure Data…

Indigenous data are completely contained by the structure.

Exogenous data reside outside the structure and are accessed through a pointer.

Copying objects

Shallow copy: a copy of pointers rather than data being pointed at

Deep copy: a copy of data being pointed at rather than the pointers.

Summary

•Vectors vs. arrays•First class objects vs. second class

objects•Ways to pass data to functions•Pointers•Dynamic memory allocation &

deallocation•“*”, “&” , “.” , “->”