Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.
-
Upload
virgil-banks -
Category
Documents
-
view
213 -
download
0
Transcript of Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.
![Page 1: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/1.jpg)
Dynamically Allocated Arrays
December 4, 2013
![Page 2: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/2.jpg)
Skip the Rest of this PowerPoint
![Page 3: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/3.jpg)
Next Time
Read Chapter 8,pp. 351-357
![Page 4: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/4.jpg)
Intro to Pointers
A pointer is a variable that holds an address Declaration uses *
int* p; int i;
& is “Address of” operator. Gets the address of a variable.
p = &i;
![Page 5: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/5.jpg)
Intro to Pointers
* is used to dereference a pointer:
*p = 15; Above: what p points to is assigned the value, 15.
pointers.cpp
![Page 6: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/6.jpg)
Arrays Are Pointers
In a program, an array, a, and a pointer, p, are the same kind of pointer.
int* p; //p is an int pointer int a[10];
They both point at an int.
![Page 7: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/7.jpg)
Arrays Are Pointers
p can get the address that a is pointing at.
p = a;
It is illegal to change a.
a = p;
Once p is assigned the value of a it can be used like an array variable.
arrayPtr.cpp
![Page 8: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/8.jpg)
Dynamically Allocated Arrays
Sometimes the amount of memory needed by an array can vary greatly. e.g. Number of campers in Ponderosa park in January
vs. August.
To save memory, use dynamic array.
![Page 9: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/9.jpg)
Dynamically Allocated Arrays
Declare using a pointer Can return to memory Can vary size
//allocate array with 20 intsint *pt = new int[20];
![Page 10: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/10.jpg)
Initialize Array
double *buff = new double[10];
for (int i=0; i<10; i++){*buff = 100.0;buff++;
}
![Page 11: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/11.jpg)
Initialize Array Alternative
double *buff = new double[10];
for (int i=0; i<10; i++){buff[i] = 100.0;
}
![Page 12: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/12.jpg)
Release memory
delete []pt;delete []buff;
If an array is no longer needed this can free up memory for other programs.
![Page 13: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/13.jpg)
Another Example int* p = new int[10];
for (int i = 0; i< 10; i++){ p[i] = i * i;}
cout << *p << " " << *(p+1) << " " << p[2] << endl;
![Page 14: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/14.jpg)
NULL
Can assign a pointer to a NULL value, which is pointing to nothing.
![Page 15: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/15.jpg)
Memory Leak
void myfunction( ){ int *pt;pt = new int[100];..//no delete
}
//in main:while(cond.) myfunction();
![Page 16: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/16.jpg)
Destructors
Return data memory When object goes out of scope.
Look at messageDest.cpp
![Page 17: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/17.jpg)
Copying Objects with Dynamic Arrays(Skip)
Assignment Operator copies objects message2 = message; Copies a pointer, not the array
Example in message.cpp Messy error.
![Page 18: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/18.jpg)
Copying Objects with Dynamic Arrays(Skip)
Deep Copying Create a new array and copy contents to the
array messageFixed.cpp
copyFrom function fixes
![Page 19: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/19.jpg)
Overload ==(Skip)
To make sure no problems happen, overload ==
Do deep copy when an assignment is done.
![Page 20: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/20.jpg)
Copy Constructor(Skip)
Copying of objects occur: Passing a copy of an argument using pass-by-
value. Returning an object from a function:return msg1;
By default these are shallow copying. Better to provide a copy constructor that does
a deep copy. messageCopyConstr.cpp
![Page 21: Dynamically Allocated Arrays December 4, 2013. Skip the Rest of this PowerPoint.](https://reader036.fdocuments.in/reader036/viewer/2022070400/56649f125503460f94c25cd7/html5/thumbnails/21.jpg)
Copy Constructor
Odd things can happen without copy constructor.
If two objects point to same array, something done to one object effects the other. Like the problem with message