Skip to main content

11.5) Dynamic Storage


cout << "How many array elements?";
cin >> size;
double* data = new double[size];
...
delete [] data;
  • The new operator can be used to allocate a specified amount of storage for a given type at run time; a pointer to the start of the storage is returned
  • After allocation, you can access the storage through the pointer as if it were an array - i.e., by using [] to index individual elements
  • It is important to release the storage using the delete operator when it is no longer required; failure to do this can lead to memory leaks (depending on the nature of your program)
  • It is often easier to use a vector than allocate storage manually in this fashion