I'm having a hard time seeing how you can safely allocate a stack located array in C++.
Normally people do this:
int a[hugeNumber]{0}; //declare,allocate,inti to 0.
That can easily fail due to stack overflow.
I would like to split up the declaration and allocation somehow and have the allocation in a try catch.
Obviously this will not work because the array would not be accesible outside of try.
try{
int a[hugeNumber];
}
catch(std::bad_alloc& e)
{
}
//code here can't use a because of scope.
If you can only allocate heap based arrays in a safe way by separating declaration and allocation then stack based arrays are unusable by production code, no?
I consider this mostly a mental exercise. I've been thinking about it and I don't see any place make the point. hugeNumber in reality is relative. Realistically even a normal number can cause a failed allocation and since there does not appear to be a way to safely allocate a stack based array I am asking the obvious... "Can stack based arrays be used in production code?". I asked just in case there is some syntax I'm not aware off. I really appreciate the input.