You can also use the new
operator to allocate built-in arrays dynamically. For example, a 10-element integer array can be allocated and assigned to gradesArray
as follows:
int *gradesArray = new int[ 10 ]();
which declares int
pointer gradesArray
and assigns to it a pointer to the first element of a dynamically allocated 10-element array of int
s. The parentheses following new int[10] value initialize the array’s elements—fundamental numeric types are set to 0
, bool
s are set to false
, pointers are set to nullptr
and class objects are initialized by their default constructors. The size of an array created at compile time must be specified using an integral constant expression; however, a dynamically allocated array’s size can be specified using any non-negative integral expression that can be evaluated at execution time.
3.133.138.14