简体   繁体   English

g++ 可变大小数组没有警告?

[英]g++ variable size array no warning?

int a;
cin >> a;
int ints[a];

Why does this not throw any kind of warning while compiling?为什么这在编译时不会抛出任何警告? How do I know when this array thing is actually using the heap or the stack?我怎么知道这个数组实际上是在使用堆还是堆栈?

g++ -std=c++11 -Wall *.cpp -o main

ISO C++ disallows the use of variable length arrays , which g++ happily tells you if you increase the strictness of it by passing it the -pedantic flag. ISO C++ 不允许使用变长数组,如果您通过向它传递-pedantic标志来增加它的严格性, g++高兴地告诉您。

Using -pedantic will issue a warning about things breaking the standard.使用-pedantic将发出有关违反标准的警告。 If you want g++ to issue an error and with this refuse compilation because of such things;如果您希望g++发出错误并因此拒绝编译; use -pedantic-errors .使用-pedantic-errors


g++ -Wall -pedantic -std=c++11 apa.cpp

apa.cpp: In function ‘int main(int, char**)’:
apa.cpp:8:13: warning: ISO C++ forbids variable length array ‘ints’ [-Wvla]
   int ints[a];
             ^
apa.cpp:8:7: warning: unused variable ‘ints’ [-Wunused-variable]
   int ints[a];
       ^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM