I'm failing to get my head around the following code from Learncpp.com:
1 2 3 4 5 6 7 8 9 10 11 12 13
int* AllocateArray(int nSize)
{
returnnewint[nSize];
}
int main()
{
int *pnArray = AllocateArray(25);
// do stuff with pnArray
delete[] pnArray;
return 0;
}
What is the function "AllocateArray" returning? Just a memory address of an array containing 25 elements? But where is the array? What's it's name? If i understand right, the code allocates some memory for a non-existant array!?.........i don't geddit!!
It seems that your confusion comes from not understanding what the new or new[] keywords do. You should learn that first if you want to understand this code.
Note that in modern C++ (specifically, C++14 and later) you should have almost no reason to ever write new or new[] because of the standard library containers and smart pointers.