Array, Pointer and Reference ( II )

# Array, Pointer and Reference ( II )

Télécharger la présentation

## Array, Pointer and Reference ( II )

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. ECE230 Lectures Series Array, Pointer and Reference ( II ) Ying Wu Electrical & Computer Engineering Northwestern University yingwu@ece.northwestern.edu

2. What to learn today? • What is a pointer? • Relation between array and pointer? • Why is pointer an angel/devil? • Pointer arithmetic • References

3. a int a; Review: variable • Three Properties • Type: • determines how many bytes are allocated for this variable by O/S • Name • is used to address (or retrieve) this variable in system memory stacks • Value • is the content and is used for calculation • What will happen if I declare a variable? • O/S will allocate (reserve) a memory block to hold the value of it • The value can be changed by “assignment” • The value can be retrieved by “addressing” • No one is able to use such a memory block until you let the O/S to recycle it.

4. Address • “We” and computers have difference languages • “We” store/retrieve information by names • Computers know exactly where the “information” is stored, i.e., the address • Physically, address can be thought as the index of memory units. So, everything in computer memory has an address. • So, how to “bridge” the gap? • Why don’t make the “addresses” accessible? • I.e., to access any content in computers’ memory through manipulating their addresses. • Not all programming language allows this. E.g., Matlab doesn’t have such a concept. • The “pointer” is such a mechanism in C/C++. • It is an angel-devil, as you will see!

5. count countPtr count 7 7 Core Concept of Pointer • Pointer variables • Contain memory addresses as their values • Normal variables contain a specific value (direct reference) • Pointers contain the address of a variable that has a specific value (indirect reference) • Indirection • Referencing a pointer value

6. Pointer Declarations and Init. • Pointer declarations • * indicates variable is a pointer int *myPtr; declares a pointer to an int, a pointer of type int * • Multiple pointers require multiple asterisks int *myPtr1, *myPtr2; • Can declare pointers to any data type • Pointers initialization • Initialized to 0, NULL, or an address • 0 or NULL points to nothing

7. yptr y y 5 500000 600000 600000 5 yPtr address of y is value of yptr Pointer Operators • & (address operator) • Returns the address of its operand • Example int y = 5;int *yPtr;yPtr = &y; // yPtr gets address of y • yPtr “points to” y

8. int a = 0; int *ptr; a 0 0x100020000 ptr 0x100020004 0xFF2F3FED a = 3; a 0x100020000 ptr 0x100020004 0xFF2F3FED 3 5 a 3 ptr = &a; 0x100020000 ptr 0x100020000 a (*ptr) = 5; 0x100020000 ptr 0x100020000 Examples

9. Question? • What are the values of pointer variables? • ?? • What are the types of the pointer variables? • A good question! • It is only meaningful to say the type of the variable the pointer pointing to! • Since the value of a ptr is an address, it can point to anything, i.e., *ptr is uncertain UNLESS we know the type! • WHY?

10. a int a; int *aptr = &a; 0 0x100020000 21493 aptr 0x100020000 double *bptr; bptr = (double*)aprt; 0 0x100020000 21493 bptr 0x100020000 Why uncertain?

11. Pointer Operators • *(indirection/dereferencing operator) • Returns the value of what its operand points to • *yPtr returns y (because yPtr points to y). • * can be used to assign a value to a location in memory *yptr = 7; // changes y to 7 • Dereferenced pointer (operand of *) must be an lvalue (no constants) • * and & are inverses • Cancel each other out *&myVar == myVar and &*yPtr == yPtr

12. 1 // Fig. 5.4: fig05_04.cpp The address of a is the value of aPtr. 2 // Using the & and * operators 3 #include <iostream> 4 The * operator returns an alias to what its operand points to. aPtr points to a, so *aPtr returns a. 5 using std::cout; 6 using std::endl; Notice how * and & are inverses 7 8 int main() 9 { 10 int a; // a is an integer 11 int *aPtr; // aPtr is a pointer to an integer 12 13 a = 7; 14 aPtr = &a; // aPtr set to address of a 15 16 cout << "The address of a is " << &a 17 << "\nThe value of aPtr is " << aPtr; 18 19 cout << "\n\nThe value of a is " << a 20 << "\nThe value of *aPtr is " << *aPtr; 21 22 cout << "\n\nShowing that * and & are inverses of " 23 << "each other.\n&*aPtr = " << &*aPtr 24 << "\n*&aPtr = " << *&aPtr << endl; 25 return 0; 26 } 1. Declare variables 2 Initialize variables 3. Print Program Output The address of a is 006AFDF4 The value of aPtr is 006AFDF4 The value of a is 7 The value of *aPtr is 7 Showing that * and & are inverses of each other. &*aPtr = 006AFDF4 *&aPtr = 006AFDF4

13. location 3000 3004 3008 3012 3016 b[0] b[1] b[2] b[3] b[4] pointer variable bptr Name of an Array is a Pointer! • Arrays and pointers are closely related • Array name is the pointer, which is pointing to the first elements of the array • Pointers can do array subscripting operations • We have an array b[5] and a pointer bPtr, as shown below • bPtr is equal to b bptr == b • bptr is equal to the address of the first element of b bptr == &b[ 0 ]

14. location 3000 3004 3008 3012 3016 v[0] v[1] v[2] v[3] v[4] pointer variable vPtr Pointer Arithmetic • Pointer arithmetic • Increment/decrement pointer (++ or --) • Add/subtract an integer to/from a pointer( + or += , -or -=) • Pointers may be subtracted from each other • Pointer arithmetic is meaningless unless performed on an array • 5 element int array on a machine using 4 byte ints • vPtr points to first element v[ 0 ], which is at location 3000 • vPtr = 3000 • vPtr += 2; sets vPtr to 3008 • vPtr points to v[ 2 ]

15. 0x1FFF2000 + 2  0x1FFF2002 ! • Confusion! • Let a pointer aptr point to an integer variable a, whose address is 0x1FFF2000. • Then I want to know now the value of aptr+2 • Strange! Aptr+2  0x1FFF2002! • WHY? • What is the answer? • 0x1FFF2004? • 0x1FFF2008? • 0x1FFF2010?

16. Pointers vs. Arrays • Accessing array elements with pointers • Element b[n] can be accessed by *( bPtr + n ) • Called pointer/offset notation • Array itself can use pointer arithmetic. • b[ 3 ] same as *(b + 3) • Pointers can be subscripted (pointer/subscript notation) • bPtr[ 3 ] same as b[ 3 ]

17. a b Reference is an Alias of a var! • Just keep it in mind: a reference is an alias of a variable, not a copy of the variable. • Declare a reference variable int a; int &b = a; • Important! Whenever b changes, a will change, since a and b are the same thing! int c = 0, a = 3; int &b = a; c = a; b = 4; Question: a = ? c = ?

18. Alert: wild pointer!!! • All C/C++ programmers have an unpleasant experience, even nightmares: a program runs well sometimes, while the same program may crash without a reason, or have very strange behaviors. • Most likely, the reason is due to a wild pointer • A wild pointer jumps here and there (which you can not predict), and ruin your program. • Why? (we’ll see it when studying functions.) • A lesson to learn: Always initialize a pointer/reference before you use it!

19. Guess and Think! • Subtracting pointers vPtr2 = &(v[ 2 ]);vPtr1 = &(v[ 0 ]); Question: vPtr2 – vPtr1 = ? Answer: 2 Why: Returns the number of elements between two addresses