How Big Do You Want That Array, Sir?
In Java, arrays are allocated with new, with an expression
to give the size. This expression can be constant or variable.
In C and and C++, the situation is far too interesting.
you should write to the standards and
not use a variable size in an array declaration.
The standard way to do this in C++ is to allocate the array with new. For
an integer variable n (or any integer expression):
- Originally, C and C++ plain array declarations, such as int fred;
required a constant size.
- Some versions of C (and some older plain C standards) allow the size
to be a variable int n; ... ; int fred[n];.
- The C++ standards have never allowed this, and the C standards are
gradually removing it.
- The gcc compiler supports them in both langauges.
int *fred = new int[n];
The @new@ looks a lot like Java at first, but there are important differences:
delete  fred;
The lack of garbage collection is a very important difference.
Programmers must remember to release memory when they are finished, or it
will continue allocated and useless until the program exits.
- Notice that the variable is a pointer instead of the array type.
- C++ does not have garbage collection, so the programmer must
explicitly delete the space when done with it.
The C++ libraries, and the language itself, provide some assistance for this
task, but it's something you must consider when using dynamic arrays
or other dynamically-allocated space.