First of all, here a
is in stack, so hopefully some_large_number
is not too large, since stack size of a thread is typically measured in single-digit megabytes.
Then 2nd thing, since you are worried about doing things right: do not use a C-style array without an actual reason. Instead, use std::vector
or std::array
or some other container class.
And 3rd thing, you can't really have memory leak with anything in the stack. You may have stack overflow, and you may have dangling pointers to things in stack which have gone out of scope, but memory leak... no.
That out of the way, having the separate blocks is certainly better for stack objects, especially when they would need to be re-initialized anyway, because simple allocation of stack is very simple operation, basically just adjusting stack pointer register. Having these small scopes will allow compiler to use the same stack space for other things between the blocks, if there's any code there. Also important, it will make clear that these are separate arrays, and no data is carried over, which can also enable compiler warnings about uninitialized variables (which you would not get if you re-used variables and forgot to re-initialize).
But, if you had items in heap, such as you actually used std::vector
, then things are a bit different. Heap allocations are more expensive, both to make and to release, so if it is a performance-critical part of code, you probably would rather re-use already created std::vector
.