0
据我所知sizeof
是编译时运算符,那么为什么此代码无需任何警告就能正确编译和运行?C++静态数组和sizeof运算符
#include <iostream>
int main() {
int size;
std::cin >> size;
int array[size];
std::cout << sizeof(array)/sizeof(int) << std::endl;
}
g++ -v
Reading specs from /usr/lib/gcc/x86_64-redhat-linux/3.4.6/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=x86_64-redhat-linux
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-9)
你对此有何评论? – 2013-03-26 10:19:35
这段代码编译和运行是否完美?你正在使用哪种编译器? – Subhajit 2013-03-26 10:19:44
可能重复[在C/C++中sizeof()的机制是什么?](http://stackoverflow.com/questions/1581839/whats-the-mechanism-of-sizeof-in-cc) – GSerg 2013-03-26 10:19:47