Pointers
-
Upload
carlos-cannon -
Category
Documents
-
view
22 -
download
0
description
Transcript of Pointers
![Page 1: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/1.jpg)
1
Pointers
(Walls & Mirrors - Beginning of Chapter 4)
![Page 2: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/2.jpg)
2
What’s a Pointer?
• A pointer variable is a variable that can contain the location of another variable as its value.
• The location of a variable is usually implemented by indicating its address in (RAM) memory.
• The location (or address) of a variable is called a pointer.
• Sometimes, for brevity, a pointer variable is simply called a pointer. You will need to be careful to understand whether pointer refers to a variable or the address of a variable.
![Page 3: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/3.jpg)
3
Pointers -“Real Life” Examples
• Suppose that your friend, Sam, borrows your copy of Walls & Mirrors. In its place, he leaves you the note
Borrowed your Walls & Mirrors book.
Thanks, Sam
• This note is like a pointer, since it it not your book, but it tells you where to go to find it.
(The paper on which the note is written is like a pointer variable.)
![Page 4: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/4.jpg)
4
Pointers - Graphical Representation
• A variable is often represented as a box.
• The value of the variable is written inside the box.
• If the variable is a pointer variable, containing a pointer, the box will contain the “tail” of an arrow that points to another variable.
Pointer variable
Other variable
![Page 5: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/5.jpg)
5
Pointers - Suggestion
• If you have a problem with pointers, draw the layout.
• It may be difficult to understand what is going on without a graphical representation of the pointer relationships.
![Page 6: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/6.jpg)
6
Pointer Declarations
int *iptr; // iptr is a pointer to an int
char *cptr; // cptr is a pointer to a char
float *fptr; // fptr is a pointer to a float
List *Lptr; // Lptr is a pointer to a List object
Sphere *Sptr; // Sptr is a pointer to a Sphere object
![Page 7: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/7.jpg)
7
Pointer Operations
• Assignment: =
– A pointer variable can be assigned only a pointer (i.e. the address of a variable) or NULL (which equals 0).
• Comparison: = =, !=
– Pointers can be compared for equality.
• Addition/Subtraction: +, – Pointers can be incremented or decremented with an integer.
• Dereferencing: *
– *ptr returns the value of the object pointed to by ptr.
• Address of: &
– &ptr returns the address of ptr (i.e. pointer to ptr).
![Page 8: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/8.jpg)
8
Pointer Operations - Address of
• The Address of operator & returns the address of an object.
float PI = 3.14159; float *PIptr;
&PI returns the address of the variable PI, not 3.14159 (the value stored in PI).
PIptr = &PI stores the address of variable PI in variable, PIptr.
&PIptr returns the address of variable PIptr.
![Page 9: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/9.jpg)
9
Pointer Operations - Dereferencing
• The Dereferencing operator * returns the value of the object to which its operand points.
float PI = 3.14159; float *PIptr; float X;
PIptr = Π // PIptr contains the address of PI
X = *PIptr; // Value stored in PI (3.14159) is
// assigned to X
*(*(&PIptr)) = *PIptr = *(&PI) = PI = 3.14159
![Page 10: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/10.jpg)
10
Pointer Initialization
int *ptr; // pointer to int declared, value undefined
int x = 5; // int declared and initialized to 5
cout << x; // prints 5
cout << *ptr; // Error! Prints undefined value, since ptr not
// initialized
ptr = &x; // ptr now contains the address of x
cout << *ptr; // prints 5
![Page 11: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/11.jpg)
11
Pointer Initialization - Suggestion
• When a pointer variable is declared it is (by default) uninitialized. Therefore, where it is pointing is undefined.
• It’s a good practice to initialize newly declared pointer variables to the NULL pointer (= 0).
– This will insure that the pointer variable is not pointing anywhere it shouldn’t.
– This will help you determine if a valid pointer has been assigned to it.
if( ptr = = NULL )
cout << “ptr has not been initialized” << endl;
![Page 12: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/12.jpg)
12
new Operator
• The operator new creates a new object of a given type.
• new returns a pointer to the newly created object.
ptr = new int;
ptr
new int variable
![Page 13: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/13.jpg)
13
new Operator (Cont’d.)
• An object created with new does not have a name and is not declared.
• An object created with new can only be used by following (dereferencing) a pointer to it.
• You need to be careful to not lose the pointer to an object created with new, since there is no other way to access it.
• Memory that was allocated with new and has become inaccessible is called a memory leak.
• For programs that run for long periods of time, memory leaks can be the reason for system failure.
![Page 14: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/14.jpg)
14
new Operator - Example 1
int *ptr; // pointer to int declared, value undefined
*ptr = 5; // Error! ptr contains invalid address and
// space for int not allocated
ptr = new int; // space for int allocated and pointer to it
// assigned to ptr
*ptr = 5; // 5 is stored in the int pointed to by ptr
![Page 15: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/15.jpg)
15
new Operator - Example 2
int *p, *q; // declare two pointer to int variables
p = new int; // allocate space for an int; make p point to it
*p = 25; // store 25 in the int pointed to by p
What is the effect of the following?
q = p;
![Page 16: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/16.jpg)
16
new Operator - Example 2 (Cont’d.)
Draw a picture!
qp
new int
25
![Page 17: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/17.jpg)
17
new Operator - Example 3
int *p, *q; // declare two pointer to int variables
p = new int; // allocate space for an int; make p point to it
q = new int; // allocate space for an int; make q point to it
*p = 35; // store 35 in the int pointed to by p
What is the effect of the following?
*q = *p;
![Page 18: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/18.jpg)
18
new Operator - Example 3 (Cont’d.)
Draw a picture!
p
new int 35
q
new int 35
![Page 19: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/19.jpg)
19
new Operator - Example 3 (Cont’d.)
What would have happened if we had executed
q = p;
instead of
*q = *p;
![Page 20: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/20.jpg)
20
new Operator - Example 3 (Cont’d.)
The new int, previously pointed to by q is LOST and cannot be recovered. This is called a memory leak.
p
new int 35 new int?
q
![Page 21: Pointers](https://reader034.fdocuments.in/reader034/viewer/2022051620/56812ff1550346895d95681c/html5/thumbnails/21.jpg)
21
Arrays and Pointers
int a[50];
int *aptr = a;
a is equivalent to &a[0]
aptr = a; is equivalent to aptr = &a[0];
aptr+5 is equivalent to &a[5]
*(aptr+5) is equivalent to a[5]