C++ creating arrays

后端 未结 7 1110
一个人的身影
一个人的身影 2021-01-11 17:49

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

相关标签:
7条回答
  • 2021-01-11 18:45

    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.

    0 讨论(0)
提交回复
热议问题