You can’t. This would have been a compiler extension in C.
However if sa is the last member of a class or struct (i.e. a VLA), then you could define it as a one-element StaticArray, manually create a larger buffer, then reinterpret that buffer through a pointer cast. (In fact this is what String does.)
@HertzDevil , Thanks. Do you think that would be any more performant than just using Arrays in the first place? (I’d have to look up how to “reinterpret that buffer through a pointer cast”.)
An Array allocates both the array itself and the buffer it points at. Slice might perform slightly better but you shouldn’t be using it over Array as a micro-optimization. Your program’s memory access patterns might matter more than the differences between stack and heap allocations.
If you know the maximum value for x*y (which you should, if you want to place it on the stack) and it’s acceptable to occupy that much space even for smaller sizes, you could just always allocate a static array with the maximum size, and then scale down according to the runtime size value.
You don’t. It is allocated either on the stack or in an object or struct instance, with a size known during compile time. That gives you certain upsides, at the cost that it cannot be resized. You can do weird shit with pointer casts as hertzdevil mentioned, but it wouldn’t help a lot.
What are you actually trying to do? What led you into this line of inquiry?
I had a similar problem, but all possible sizes for the array were known, so I ended up with a massive case allocating the array for each possible size required.