g++ variable size array no warning?

匿名 (未验证) 提交于 2019-12-03 02:36:02

问题:

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

回答1:

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.

Using -pedantic will issue a warning about things breaking the standard. If you want g++ to issue an error and with this refuse compilation because of such things; use -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];        ^ 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!