C++ creating arrays

后端 未结 7 1156
一个人的身影
一个人的身影 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.

提交回复
热议问题