The C++ Data Types
Dynamic storage Allocation with the new and delete operators
Pointers can be used to create arrays or objects of any size at run time with
dynamic memory allocation
The new operator: new type; allocates memory storage for an object and returns
the address of that memory: new int; new double;
for an array of objects: new type [number_of_elements]; new int [1000];
the return value from new is used to initialize pointers:
double *pd = new double[100]; // allocates memory for 100 double elements
int n = 1000; int *p = new int [n]; // notice that n is not a const
int size; cout << “input array size”; cin >> size; float *pf = new float[size];
The delete operator: delete pointer_to_object; deallocates the storage of an object
delete [ ] pointer_to_array; is used when deleting an array
delete[ ] pd; delete [ ] pf; // for int, float, double, and char types [ ] is not needed
delete is used to avoid memory leaks which occur when allocating storage for
objects defined within a function and exiting the function.