In C++ my understanding is that new should be used to allocate a single variable dynamically, and that new[] should be used to dynamically allocate an array of variables.
However the following compiles and runs fine in Xcode:
#include <iostream>
int main()
{
std::cout<<"Enter length of array: ";
int length;
std::cin>>length;
int *mArray = new int;
for(int i=0;i<length;++i)
mArray[i] = i;
for(int i =0;i<length;++i)
std::cout<<mArray[i]<<std::endl;
delete mArray;
return 0;
}
Why does this work? Shouldn't it cause the program to crash if it is accessing memory it hasn't been allocated?