[ fromfile: memoryaccess.xml id: arraysummary ]
Following is a list of the most important points that we have raised in this chapter.
An array is a sequence of contiguous memory cells, all of the same size.
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.
Array subscripts are valid only when used to access members of an array and only within the declared limits of the array.
The standard does not guarantee that the compiler can catch attempts to use the subscript operator with a pointer that is not an array.
Arrays are passed to and returned from functions as pointers.
It is possible to apply the arithmetic operators +
, -
, ++
, and --
to an array pointer, subject to sensible limitations.
The results of pointer arithmetic are undefined outside the context of an array.
The standard does not guarantee that the compiler can catch attempts to misuse pointer arithmetic.
Pointers can acquire values only in the following ways:
By initialization when they are first created.
By assignment after they exist.
As a result of pointer arithmetic.
A dynamic array of size
elements of ArrayType is allocated using the syntax
uint size; ArrayType* pt; pt = new ArrayType[size] ;
Each element of the dynamic array is given default initialization when the array is allocated.
To deallocate such a dynamic array, it is necessary to use the syntax:
delete[] pt;
The ANSI/ISO standard requires the free store operator new
to throw a bad_alloc
exception instead of returning NULL
if it
cannot carry out an allocation request.
For more details about exceptions, see the separate article on that subject in our dist directory.
The qualified operator new (nothrow)
can return 0 if it cannot carry out an
allocation request.
Dynamic arrays should be carefully encapsulated in classes that are designed with proper destructors, copy
constructors, and copy assignment operators.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |