3

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.

30
  • 1
    It's certainly difficult to use large stack-allocated arrays in portable code, at any rate. As long as you don't have recursion involved, you can do some testing to find the greatest stack depth used, and then tell the linker how much stack space you need allocated. Commented Jan 9, 2018 at 2:08
  • 2
    Just use std::vector<int> a(hugeNumber); Commented Jan 9, 2018 at 2:08
  • 1
    I don't think stack overflow throws exception. Commented Jan 9, 2018 at 2:14
  • @EdHeal: Though that does involving initializing all the entries immediately, which a raw array need not do for POD types. That said, large enough heap allocations may draw directly from the OS (which gives back zeroed memory "for free"), and a standard lib which takes advantage of this may be able to skip the zeroing work in that case. Commented Jan 9, 2018 at 2:18
  • 2
    Some programmers could leak memory, or dereference dangling pointers - there's no shortage of dumb things one can do in C++. I don't think misuse of stack arrays ranks very high on the list of common mistakes. Commented Jan 9, 2018 at 3:11

2 Answers 2

6

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...

This applies to all automatic variables, not just arrays. If you cannot be sure that there is enough stack memory for a "normal" sized array, then how could you be sure if there is enough memory for a single object?

Stack memory is limited, but it is still huge compared "normal" sized objects and arrays.

Can stack based arrays be used in production code?

Sure. Automatic arrays can be used in production code, just as any automatic objects can (and in fact, must) be used in production. You simply cannot use huge automatic objects.

Exactly where the practical size limit of an automatic object is, is greatly situational. The more automatic storage you use, the more relevant it is to analyse the maximum stack size used.

To avoid stack overflow in production, there should be testing before deployment. There are tools that will detect a stack overflow if it occurs in a test run.


Obviously this will not work because the array would not be accesible outside of try.

try{
    int a[hugeNumber];
}
catch(std::bad_alloc& e)
{
}

It will also not work because stack overflow does not throw an exception.


I asked just in case there is some syntax I'm not aware off.

There is no syntax to "try" allocating automatic objects (including arrays). There is also no way in C++ to check how much memory there is available for automatic storage although there might be system specific ways.

Sign up to request clarification or add additional context in comments.

2 Comments

Right, fairly good answer based on discussion in the thread. I think there is a diff between single item stack objects like int and an array of items. Namely that you will unlikely run into a scenario that you will ever write a code where you have enough single items to blow the stack. That code would have so many variables it would be humanly untraceable. On the other hand, nothing prevents you from declaring a stack allocated array of any size you wish. Really what we're asking here is it possible to seperate declaration and allocation or use some other scheme to verify succ allocation.
Most people don't bother verifying alloc. The q was, "is it possible?". The answer is, it is not. It is the lack of possibility that I was asking. Not if we should or should not take a chance. I blew the stack at 1MB. Could have blown it at much less with certain sys. If you can make the ans a bit more clear to reflect this, I would like to give you the points cuz your ans is nice and will serve others well. I don't think, "yes, you can use stack allocated arrays safely because their just like any other stack variable, just keep the size 'small' "... really does justice to the issue.
0

I would like to split up the declaration and allocation somehow and have the allocation in a try catch.

That requires dynamic allocation using new, eg:

int *a = nullptr;
try
{
    a = new int[hugeNumber];
}
catch(const std::bad_alloc &e)
{
    ... 
}
...
delete[] a;

Or:

std::vector<int> a;
try
{
    a.resize(hugeNumber);
}
catch(const std::bad_alloc &e)
{
    ... 
}
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.