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