When I started programming with C++ I was learned to allocate the array size by using dynamic memory allocation as follows:
int main()
{
int nArrayLength;
cout << "Enter an array length: ";
cin >> nArrayLength;
int *nArray = new int[nArrayLength];
// contents
delete[] nArray;
return 0;
}
Now I tried the following code using Code::Blocks 12.11 with the mingw32-g++ // GNU GCC compiler.
int main()
{
int nArrayLength;
cout << "Enter an array length: ";
cin >> nArrayLength;
int nArray[nArrayLength];
return 0;
}
This also works fine.
Therefore, why should I use dynamic memory allocation in this case when the easier method also works fine?