thursday, january 11, 2007 harrisberger's fourth law of the lab: experience is directly...

47
Thursday, January 11, 2007 Harrisberger's Fourth Law of the Harrisberger's Fourth Law of the Lab: Lab: Experience is directly Experience is directly proportional to the amount proportional to the amount of equipment ruined of equipment ruined .

Post on 20-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Thursday, January 11, 2007

Harrisberger's Fourth Law of the Lab:Harrisberger's Fourth Law of the Lab: Experience is directly Experience is directly

proportional to the amount of proportional to the amount of equipment ruinedequipment ruined.

Most of C++’s power is derived from pointers.

They allow different sections of code to share information easily.

Pointers enable complex data structures like linked lists.

Pointers

Assumptions:characters are one byte in lengthintegers are four bytes longfloats are four bytes longdoubles are eight bytes long

Pointers

A pointer is a variable that holds a memory address.

Pointers

int x=5;

int *xptr;

5 x of type int0x0012F578

0x0012F690 xptr of type int*

int x=5;

int *xptr;

xptr=&x; //points to x

5

0x0012F578

x of type int0x0012F578

0x0012F690 xptr of type int*

int x=5;

int *xptr;

xptr=&x; //points to x

5 x of type int0x0012F578

xptr of type int*

int x=5;

int *xptr;

xptr=&x; //points to x

5

x of type intxptr of type int*

int x=5;

int *xptr;

xptr=&x; //points to x

The most common error is forgetting to initialize the pointer

5

x of type intxptr of type int*

There are two special operators that are used with pointers: * and &

The & is a unary operator that returns the memory address of its operand.

int balance = 350;int *balptr;balptr = &balance;

Pointer Operators

The second operator * is the complement of &. It is a unary operator that returns the value of variable located at address specified by its operand.

int balance = 350;int *balptr;balptr = &balance;int value;value = *balptr; //what does value contain?

Pointer Operators

& address of operator

* value of pointee(de-referencing operator)

Pointer Operators

When a pointer is first allocated it does not point to anything.

Trying to de-reference an un-initialized pointer is a serious runtime error.

If you are lucky the dereference will crash or halt immediately.

If you are unlucky it will corrupt a random area of memory – so that things go wrong after some indefinite time.

Pointers

Make a memory drawing!

Pointers

int main() { int balance; int *balptr; int value; balance = 3200; balptr = &balance; value = *balptr; cout << "balance is: " << value << '\n'; cout<< "Memory address where balance is stored is: "

<<balptr<<endl; return 0;}

Pointer Operators

Output

balance is: 3200

Memory address where balance is stored is: 0012FF7C

(Note memory address may be different when you run it on your machine)

Another example

Three things to remember…

int a, b;

int *aptr, bptr; //Be careful!

value = *balptr;The compiler transfers the proper number of bytes according to base type.

int *p;double f;// ...p = &f; // ERROR

Base Type

int *p;int x=12;p=&x;cout<<x<<endl;//Assign a value to the location pointed to by p*p = 101;cout<<x<<endl; //what is value of x?cout<<*p<<endl;cout<<p<<endl;cout<<&x<<endl;

Assigning values through a pointer

int *p;int x=12;p=&x;cout<<x<<endl; //prints 12//Assign a value to the location pointed to by p*p = 101;cout<<x<<endl; //prints 101cout<<*p<<endl; //prints 101cout<<p<<endl; //prints 0012FF78cout<<&x<<endl; //prints 0012FF78

(Note memory address may be different when you run it on your machine)

Assigning values through a pointer

(*p)++; //increment value at the location pointed to by p

int main() { int *p, num; p = &num; *p = 454; cout << num << ' '; (*p)++; /*parentheses are necessary because *

operator has lower precedence than ++ operator */

cout << num << ' '; (*p)--; cout << num << '\n'; return 0; } //Output?

Assigning values through a pointer

//Output is 454 455 454int main() { int *p, num; p = &num; *p = 454; cout << num << ' '; (*p)++; cout << num << ' '; (*p)--; cout << num << '\n'; return 0;}

Assigning values through a pointer

There are only four arithmetic operators that can be used on pointers: ++, --, +, -

Let p1 be an integer pointer which contains the address 5000p1++;

Each time p1 is incremented, it shall point to the next integer.p1--;

Pointer Arithmetic

There are only four arithmetic operators that can be used on pointers: ++, --, +, -

Let p1 be an integer pointer which contains the address 5000p1++; // now p1 will be 5004

Each time p1 is incremented, it shall point to the next integer.p1--; will cause p1 to be 4996 if initially it was 5000.

Pointer Arithmetic

int main() { int *aptr, a[5];

double *bptr, b[5];int i;aptr = a; bptr = b; for(i=0; i<5; i++){

aptr = a+i;bptr = b+i;cout << aptr << " " << bptr << endl;

}

return 0; }

Pointer Arithmetic

0x0012FEB8 0x0012FE7C0x0012FEBC 0x0012FE840x0012FEC0 0x0012FE8C0x0012FEC4 0x0012FE940x0012FEC8 0x0012FE9C

Pointer Arithmetic

Let p1 be an char pointer which contains the

address 4000

p1++; // now p1 will be 4001

Each time p1 is incremented, it shall point to

the next character.

p1--; will cause p1 to be 3999 if initially it was

4000.

Pointer of type other than char shall increase

or decrease by length of base type.

Pointer Arithmetic

•You cannot add two pointers

•You can subtract two pointers (if they are of same base type).

•Other than addition or subtraction of a pointer and an integer, or the subtraction of two pointers, no other arithmetic operations can be performed on pointers.

•You cannot add or subtract float or double values.

Pointer Arithmetic

int main() { int *aptr, a[5];

double *bptr, b[5];int i;aptr = a; bptr = b; for(i=0; i<5; i++){

a = aptr+i;b = bptr+i;cout << a << " " << b << endl;

} return 0; }

What is wrong here?

In C++, there is a close relationship between pointers and arrays.

Pointers and Arrays

In C++, there is a close relationship between pointers and arrays.

Name of array, without an index, is a pointer to first element of the array.

Pointers and Arrays

int main() { int *i; int i_array[4]={55, 26, 17, 68}; i=i_array; cout << i << " " << i_array << “\n”; cout << i[0] << " " << i_array[0] << “\n”; cout << *(i+1) << " " << i_array[1] <<“\n”; return 0;} //Output?

Pointers and Arrays

Output is:

0012FF6C 0012FF6C55 5526 26

Pointers and Arrays

int i1[4]={55,26,17,68};int i2[4]={11, 2, 53, 14};i2=i1; //NOT ALLOWED

char str1[]=“i am a string”;char str2[]=“i am a string”;

Pointers may be compared in C++ using relational operators like >, >=, <, <=, == etc, but they must have some relationship to be meaningful

Pointers and Arrays

int i1[4]={55,26,17,68};int i2[4]={11, 2, 53, 14};i2=i1; //NOT ALLOWEDWhy? /*name of array is a constant that points to beginning of array*/

char str1[]=“i am a string”;char str2[]=“i am a string”;str1==str2; //WRONG way of string comparison

Pointers may be compared in C++ using relational operators like >, >=, <, <=, == etc, but they must have some relationship to be meaningful

Pointers and Arrays

int i1[4]={55,26,17,68};int i2[4]={11,2,53,14};

int *p1;p1 = i1;int *p2;p2 = p1; //this is ok

Pointers and Arrays

int *y_ptr, y;

//y is of type int

// y_ptr is pointer to int

y=45;

y_ptr=&y;

int *y_ptr, *another_ptr, y=45;

y_ptr=&y;

another_ptr=&y; /*what is *another_ptr

what is *y_ptr*/

Pointers

int b[] = {10, 20, 30, 40}; int *bPtr; bPtr = b; // set bPtr to point to array b cout << "Array b printed with:" << endl << "Array subscript notation" << endl;

for (int i = 0; i < 4; i++) cout <<b[i] << endl;

cout << "Pointer subscript notation" << endl;

for (i = 0; i < 4; i++) cout << bPtr[i] << endl; //Another way to do this?

Using subscripting and pointer notations with arrays

int offset; cout << "Pointer/offset notation where" << endl << "the pointer is the array name" << endl;

for(offset = 0; offset < 4; offset++) cout << *(b + offset) << endl;

cout <<"Pointer/offset notation" << endl;

for(offset = 0; offset < 4; offset++) cout << *(bPtr + offset) << endl;

Using subscripting and pointer notations with arrays

int *i; int int_array[4]={5,6,7,8}; i=int_array;

cout << i << " " << int_array<<endl;

cout << i[0] << " " << int_array[0]<< endl ; cout << &i[0] << " " << &int_array[0]<< endl;

Using subscripting and pointer notations with arrays

0x0012FEBC 0x0012FEBC5 50x0012FEBC 0x0012FEBC

Using subscripting and pointer notations with arrays

int a[5]={12, 3, 45, 5, 8};int *ptr;ptr=a;cout<<a[0]<<endl;cout<<ptr[0]<<endl;cout<<*(a+0)<<endl;cout<<*(ptr+0)<<endl;

cout<<a[1]<<endl;cout<<ptr[1]<<endl;cout<<*(a+1)<<endl;cout<<*(ptr+1)<<endl;

Using subscripting and pointer notations with arrays

int a[5]={12, 3, 45, 5, 8};int *ptr;ptr=a;cout<<a[0]<<endl; //prints 12

cout<<ptr[0]<<endl; //prints 12

cout<<*(a+0)<<endl; //prints 12

cout<<*(ptr+0)<<endl; //prints 12

cout<<a[1]<<endl; //prints 3

cout<<ptr[1]<<endl; //prints 3

cout<<*(a+1)<<endl; //prints 3

cout<<*(ptr+1)<<endl; //prints 3

Using subscripting and pointer notations with arrays

int i1[4]={55, 26, 17, 68};What is wrong with the following statement?i1++;

//The following is okint *i_ptr;i_ptr = i1;i_ptr++;cout<<*i_ptr;

//The following is ok *(i1+3) = 100; // This is OK because i1 has not changed

Pointers and Arrays

int b[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; int *bPtr; bPtr= b; // set bPtr to point to array b /* set b2Ptr to point to sixth element of array b */ int *b2ptr; b2ptr = b+5; //Remember pointer arithmetic? cout << "b[" << 5 << "] = " << b[5] << endl; cout << "*b2ptr= " << *b2ptr << endl; cout << "(b2ptr - bPtr) = " << (b2ptr - bPtr);

Pointers and Arrays

int b[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; int *bPtr; bPtr= b; // set bPtr to point to array b /* set b2Ptr to point to sixth element of array b */ int *b2ptr; b2ptr = b+5; //Remember pointer arithmetic? cout << "b[" << 5 << "] = " << b[5] << endl; cout << "*b2ptr= " << *b2ptr << endl; cout << "(b2ptr - bPtr) = " << (b2ptr - bPtr) << endl;

Output is:b[5] = 60*b2ptr= 60(b2ptr - bPtr) = 5

Pointers and Arrays