就怕这种23400000000123的吧
确实 lhgzbxhz 发表于 2020-11-8 10:33
这貌似是c99定义的标准,在c++标准下无法运行
为获得最大程度的可移植性,请尽量使用new或malloc
谢谢,看来是我弄混乱了。因为我主要用的是g++的编译器。并且之前也一直用的C语言 谢谢大家了{:5_95:} https://stackoverflow.com/questions/19556181/c-expression-must-have-constant-value
C++ doesn't allow variable length arrays. The size must be a constant. C99 does support it so if you need you can use a C99 compliant compiler. Some compilers like GCC and Clang also support VLA as an extension in C++ mode
But if C++ is a must then you can use alloca (or _alloca on Windows) to allocate memory on stack and mimic the C99 variable length array behavior
Amta = (int *)alloca(sizeof(int) * size);
This way you don't need to free the memory after going out of scope because the stackframe will automatically be restored. However you need to be very careful while using this. It's still better to use std::vector in C++ for these purposes
页:
1
[2]