Ah the wonders of
dynamic memory needs.
There are two ways to create and array. You can create a static array inside the local memory space:
int array[100]; //creates an array of 100 elements.the problem with this is that it is static. You hard code into your program the size of the array. When the program loads, it sets that much memory aside (which can eat away at the memory available to your program for function calls and other needs.). (Note that if you create a static array in a function then it is allocated on the stack when the function is called, so for example if you call the function recursively that can eat up memory fast).
There is a more dynamic way. You can ask the operating system for a chunk of free memory using the malloc() in C or the new in C++.
char *array = new (nothrow) int[100]; //allocates a dynamic array...note: the '(nothrow)' part just tells the compiler not to use C++'s exception handling ability if there is an error. It is actually better to use the exceptions.The new[] operator needs to be paired with the delete[] operator which releases the memory back to the operating system. (note the 'new' operator can be used to allocate a single object, and it is paired with the 'delete' operator, but arrays use the 'new[]' operator and 'delete[]' operator).
delete[] array;// return the memory to the OSNow the new operator does not have a fancy "re-Allocate" ability, but you can write your own routine to allocate a new array, copy the data over, and then delete the old one.
A better solution is to use a
vector object to handle all your array needs.