Why can\'t I do something like this:
int size = menu.size;
int list[size];
Is there anyway around this instead of using a vector? (arrays a
As other have said, the C++ language designers have chosen not to allow variable length arrays, VLAs, in spite of them being available in C99. However, if you are prepared to do a bit more work yourself, and you are simply desperate to allocate memory on the stack, you can use alloca()
.
That said, I personally would use std::vector
. It is simpler, safer, more maintainable and likely fast enough.