The problems with fixed size arraysDeclaring an array with a fixed size like
int a;has two typical problems:
- Exceeding maximum. Choosing a real maximum is often impossible because the programmer has no control over the size of the data sets the user is interested in. Erroneous assumptions that a maximum will never be exceeded are the source of many programming bugs. Declaring very large arrays can be extremely wasteful of memory, and if there are many such arrays, may prevent the program from running in some systems.
- No expansion. Using a small size may be more efficient for the typical data set, but prevents the program from running with larger data sets. If array limits are not checked, large data sets will run over the end of an array with disastrous consequences. Fixed size arrays can not expand as needed.
newoperator to allocate memory, and
deleteto free memory that is no longer needed.
This is exactly what is
vectordoes, but let's see how it's done with an array.
Declare array as a pointer, allocate with To create a variable that will point to a dynamically allocated array, declare it as a pointer to the element type. For example,
int* a = NULL; // pointer to an int, intiallly to nothing.A dynamically allocated array is declared as a pointer, and must not use the fixed array size declaration. The above declaration creates a pointer, but doesn't yet allocate any memory to it.
Allocate an array with code>newWhen the desired size of an array is known, allocate memory for it with the
newoperator and save the address of that memory in the pointer. Remember: Pointers may be subscripted just as arrays are. The example below reads in a number and allocates that size array.
int* a = NULL; // Pointer to int, initialize to nothing.
int n; // Size needed for array
cin >> n; // Read in the size
a = new int[n]; // Allocate n ints and save ptr in a.
for (int i=0; i
a[i] = 0; // Initialize all elements to zero.
. . . // Use a as a normal array
delete  a; // When done, free memory pointed to by a.
a = NULL; // Clear a to prevent using invalid memory reference.
Freeing memory with deleteWhen you are finished with dynamically allocated memory, free it with the delete operator. After memory is freed, it can be reused by later
newrequests. Memory that your program didn't free will be freed when the program terminates. Never free memory that wasn't dynamically allocated - the results are unpredictable.
delete  a; // Free memory allocated for the a array.
a = NULL; // Be sure the deallocated memory isn't used.
Use You must specify "
 when deleting arrays
" when deleting an array, but not for a single value. It isn't possible to delete only part of an array.
Do you have to reset a pointer after delete?Following the
deletein these examples, I reset the pointer to
NULL. This isn't strictly necessary, but it's very good practice so that any use of the pointer will produce an error. Attempts to use memory location 0, which is the normal default value of
NULL, will be blocked by the way most operating systems allocate memory.
deletereset the pointer? It does in some systems, but the language specification does not require it, so not all systems do it.