1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled...
-
Upload
augustus-dixon -
Category
Documents
-
view
232 -
download
3
Transcript of 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled...
![Page 1: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/1.jpg)
1
CMSC 202
Pointers
Dynamic Memory Allocation
![Page 2: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/2.jpg)
2
A simple variable
• A variable is drawn as a labeled boxint x;
X :
![Page 3: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/3.jpg)
3
Complex Variables
• A complex variable is drawn as complex boxes (horizontal for array, vertical for struct)struct Astruct {
int y; char c;
};int intArray [ 4 ];
Astruct myStruct;
intArray:
myStruct :
![Page 4: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/4.jpg)
4
Pointer value
• A pointer value is drawn as an arrow. The arrow points to a picture of the object to which the pointer points. The pointer is said to reference the object.
3
![Page 5: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/5.jpg)
5
Pointer Variable
• A pointer variable is drawn as a labeled box, like any other simple variable
int *iPtr;
• NULL is a pointer value that doesn’t point to anything and is drawn as a box with a slash
iPtr:
![Page 6: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/6.jpg)
6
When a pointer variable points to an object …
… the base of the arrow representing the pointer value is placed in the pointer variable’s box
42iPtr :
![Page 7: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/7.jpg)
7
Assignment to pointer variables
• A pointer variable receives a value through assignment.– Newly allocated space (using new)– Previously allocated space
![Page 8: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/8.jpg)
8
Allocating space with “new”
• Graphically depict the result of allocating memory with new by
1. Figure out what kind of space is being allocated
2. Draw a box to represent the allocated space
3. Draw an arrow pointing to the new box and place it’s base in the box representing the pointer variable being assigned to
![Page 9: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/9.jpg)
9
A “new” picture
class Car
{
public:
. . . .
private:
string m_color;
int m_mileage;
};
Car *CarPtr;
CarPtr = new Car;
m_color
m_mileage
CarPtr
![Page 10: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/10.jpg)
10
What’s in the “new”data?
• Nothing– Conceptually, after you use new, you get some
blank space to fill with data
• Something– Your new space is not initialized to some zero
value. Try allocating some new memory with new and immediately printing its contents.
– You will see all sorts of random values. This is related to a security concern we will discuss later.
![Page 11: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/11.jpg)
11
Pointer Assignment
Suppose ‘thing1’ and ‘thing2’ are
“pointers to int” and ‘thing1’ points to an int whose value is 17.
17thing1:
thing2:
![Page 12: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/12.jpg)
12
Pointer Assignment (cont’d)
And now execute the statement
thing2 = thing1;1. Draw a new arrow that points to the box being
referenced by the pointer value on the right hand side
2. Place the the base of the new arrow in the box representing the pointer variable on the left hand side
17thing1:
thing2:
![Page 13: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/13.jpg)
13
Dereferencing pointers
Given the following picture
17thing1:
The * (star) operator “deferences” a pointer variable. In terms of our picture it means “follow the arrow”.
We can use dereferencing to change what a pointer value points to:
*thing1 = 42; results in this picture
42thing1:
![Page 14: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/14.jpg)
14
An exercise for the studentGiven the following declarations and codetypedef int* intPtrArray [ 3 ];typedef intPtrArray* arrayPtr;arrayPtr cat;int *dog;dog = new int;*dog = 42;cat = new intPtrArray;cat[0] = dog;cat[1] = NULL;cat[2] = new int;*(cat[2]) = 17;
![Page 15: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/15.jpg)
15
The Questions
• What type of variable is dog?
• What type of variable is cat? (NO, the answer is NOT arrayPtr).
• Draw the picture that results from the code
![Page 16: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/16.jpg)
16
The & operator
• The & (address of) operator is used to create an arrow that points to an existing box. Consider the following code
int value;
int *pValue;
value = 17;pValue = &value;
17value:
pValue:
The expression &value gives us a pointer to value
![Page 17: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/17.jpg)
17
Pointers and Arrays
• A strong relationship between pointers and arrays.– The name of an array is defined to be a pointer to the
the first (0th) element of the array
Consider the declaration int bongo [ 4 ];
The name bongo can be used as a pointer to reference bongo[0] .
*bongo = 42;
is equivalent to bongo[0] = 42;
![Page 18: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/18.jpg)
18
Pointers to array elements
foo:
bar:
0 1 2 3 4 5
foo + 1
If a pointer value references a particular array element, then adding an integer, n , to that pointer value will result in a new pointer value that references the array element n elements way from the original element
![Page 19: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/19.jpg)
19
Putting these together…
… we observe that the nth element of an array called myArray can be referenced as
myArray + n
Thus, the following expressions are equivalent
myArray [n] and *(myArray + n)
&myArray [ n ] and myArray + n
![Page 20: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/20.jpg)
20
Using ‘delete’
• delete is the operator which is used to recycle memory allocated with new
• The forms of new and delete must match// for a single objectint * x = new int;delete x;
// for an array of objectint *intp = new int [ 7 ];delete [ ] intp;
![Page 21: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/21.jpg)
21
A delete example
Suppose ‘soup’ is a pointer variable that references an int containing 42
soup: 42
Then the statementdelete soup
recycles the space referenced by soup.
soup: 42
![Page 22: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/22.jpg)
22
A potential problem
int *soup, *salad;
soup = new int ( 42 );
salad = soup;
delete soup;
What does the picture of memory look like?
What’s the potential problem?
![Page 23: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/23.jpg)
23
A potential security problem
• Recall that newly allocated space is filled with random data.– Where does that data come from? It’s not truly
random, but it is whatever old data was left behind when some program used delete. Your deleted data is not gone; it is floating around somewhere in the computer’s memory, to be reallocated when another program needs the space.
![Page 24: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/24.jpg)
24
A potential security problem
• So, what happens to the data in a credit card number when you delete it? What if some clever hacker could figure out a way to get that deleted memory reallocated to another program?– To avoid this problem, any sensitive data
should have its value changed (set to zero) before calling delete.
![Page 25: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/25.jpg)
25
Multi-dimensional Arrays
We declare a static 2-dimensional array aschar chArray1 [ 3 ]
[ 4 ];
What we envision is
0
1
2
0 1 2 3
chArray1 :
![Page 26: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/26.jpg)
26
Static 2-D array in Memory
What we get in memory ischArray1:
row 0 0 0 0 1 1 1 1 2 2 2 2 column 0 1 2 3 0 1 2 3 0 1 2 3
We use double subscripts ( chArray [ 2 ] [ 1 ] ) to reference the elements by row/column. The compiler uses the row number, column number, number of columns and array type to calculate a memory address.
![Page 27: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/27.jpg)
27
A dynamic 2-D array
To dynamically allocate a 2-D array, we must create an array of pointers (the rows) in which each row element points to an array of elements (the columns).
We need only declare the name of the dynamic array correctly
char **chArray2;
![Page 28: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/28.jpg)
28
char **chArray2
This declaration is read as
“chArray2 is a pointer to a pointer to a char”
In reality, the compiler doesn’t “know” if you plan to use chArray2 to point to one pointer to char or as the name of an array of many pointers to char.
When we use it for dynamic 2-D arrays, we use it to point to an array of pointers to char
![Page 29: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/29.jpg)
29
The code
// the array of pointers for the rowschArray2 = new char* [ 3 ];
// now allocate the elements of each rowfor (int r = 0; r < 3; r++)
chArray2 [ r ] = new char [ 4 ];
![Page 30: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/30.jpg)
30
Dynamic 2-D Array in Memory
chArray2: 0
1
2
0 1 2 3
We can still use double subscript ( chArray [ 2 ] [ 1 ] ) to access the elements. The compiler uses pointer arithmetic to find the memory address.
![Page 31: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/31.jpg)
31
delete’ing a dynamic array
To deallocate a dynamically allocated multi-dimensional array, we must deallocate memory in the reverse order that it allocated
for (int r = 0; r < 3; r++)
delete [ ] chArray2[ r ];
delete [ ] chArray2;
![Page 32: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/32.jpg)
32
Exercises for the student
1. Draw the results of the following codechar *fischer;fischer = new char[3];fischer[0] = ‘p’;fischer[1] = ‘k’;fischer[2] = ‘4’;
![Page 33: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/33.jpg)
33
More exercises2.Draw the results of the following codestruct soprano {
int x;char ch[3];
};typedef soprano* alto;
soprano *jane;alto *joe;jane = new soprano;jane->x = 1;jane-> ch[ jane->x ] = ‘G’;joe = new alto[4];joe[0] = new soprano;joe[0]->ch[2] = ‘Q’;joe[3] = jane;
![Page 34: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/34.jpg)
34
Even more exercises
3. Write the typedefs, declarations and code that will create the picture below
foo:
1742
![Page 35: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/35.jpg)
35
Still more
4. Write the typedefs, declarations and code that will produce the picture below
foo: 16
bar: 16
![Page 36: 1 CMSC 202 Pointers Dynamic Memory Allocation. 2 A simple variable A variable is drawn as a labeled box int x; X :](https://reader035.fdocuments.in/reader035/viewer/2022062515/56649f3f5503460f94c5fec8/html5/thumbnails/36.jpg)
36
And one last exercise
5. Write the typedefs, declarations and code that will produce the picture below.
tom:
5