[ fromfile: memoryaccess.xml id: arrays ]
When an array is declared, the size of the array must be made known.
This can be done explicitly or by initialization:
int a[10]; // explicitly creates uninitialized cells a[0], a[1],..., a[9] int b[] = {1,3,5,7}; // implicitly creates and initializes b[0],..., b[3]
The array name is an alias for a const
typed pointer to the first cell of the array.
There is no automatic default initialization of pointer variables.
Array indices are relative offsets from the base address:
a[k] is equivalent to *(a + k)
There is a special syntax for defining a dynamic array consisting of a given number of elements of some type.
uint n; ArrayType* pt; pt = new ArrayType[n];
Each element of the newly allocated array is given default initialization.
To properly deallocate this array, it is necessary to use the syntax:
delete[] pt;
We discuss exceptions and what happens when a request for dynamic memory cannot be fulfilled by the system in a separate article[86] in our dist directory.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |