阵列的动态分配 为什么C++中不可能通过数组动态分配来编译下面的代码? 在取消注释注释时显示错误 #include<iostream> #include <string> using namespace std; int main() { string aa; cin>>aa; int a[aa.size()];// though allocating the array dynamically the compilation succeeded cout<<"COMPILATION SUCCESS"<<endl; /*char *p; cin>>p; int y=sizeof(p); int b[y]; cout<<"COMPILATION ERROR"<<endl; */ /* int tt; cin>>tt; int c[tt];// shows error cout<<"ERROR"; */ } #包括 #包括 使用名称空间std; int main() { 字符串aa; cin>>aa; int a[aa.size()];//尽管动态分配数组,编译还是成功了 CUT< P>因为你使用的是编译器。C++中的VLAs是GNU扩展,你有可能编译成这个代码: G++或 CLAN++?

阵列的动态分配 为什么C++中不可能通过数组动态分配来编译下面的代码? 在取消注释注释时显示错误 #include<iostream> #include <string> using namespace std; int main() { string aa; cin>>aa; int a[aa.size()];// though allocating the array dynamically the compilation succeeded cout<<"COMPILATION SUCCESS"<<endl; /*char *p; cin>>p; int y=sizeof(p); int b[y]; cout<<"COMPILATION ERROR"<<endl; */ /* int tt; cin>>tt; int c[tt];// shows error cout<<"ERROR"; */ } #包括 #包括 使用名称空间std; int main() { 字符串aa; cin>>aa; int a[aa.size()];//尽管动态分配数组,编译还是成功了 CUT< P>因为你使用的是编译器。C++中的VLAs是GNU扩展,你有可能编译成这个代码: G++或 CLAN++?,c++,memory-management,dynamic,C++,Memory Management,Dynamic,将编译器设置为严格的ISO C++模式,它会警告或出错。 我从clang++中得到了什么: h2co3-macbook:~ h2co3$ clang++ -o quirk quirk.cpp -Wall -std=c++11 -pedantic quirk.cpp:6:9: warning: variable length arrays are a C99 feature [-pedantic,-Wvla] char cs[s.size() + 1]; ^ quir

将编译器设置为严格的ISO C++模式,它会警告或出错。 我从
clang++
中得到了什么:

h2co3-macbook:~ h2co3$ clang++ -o quirk quirk.cpp -Wall -std=c++11 -pedantic
quirk.cpp:6:9: warning: variable length arrays are a C99 feature [-pedantic,-Wvla]
    char cs[s.size() + 1];
           ^
quirk.cpp:6:7: warning: unused variable 'cs' [-Wunused-variable]
    char cs[s.size() + 1];
         ^
2 warnings generated.

你确定它是
cintt;
?数组的大小必须在编译时就知道。在C++14中,它们是标准的。@Fanael:在C++14中,VLA和dynarray(另一种选择)都在考虑之中,但在标准真正出台之前,我不会说“它们是标准的”。