當前位置: 首頁IT技術 → sizeof 的使用讓程序多一個標記

sizeof 的使用讓程序多一個標記

更多

#include #include int main() { struct { int a ; char b ; int c ; } a,*b; int d; char f; float g; double h; printf("sizeof(a) = %d,sizeof(b) = %d ,sizeof(*b) = %d\n",sizeof(a),sizeof(b),sizeof(*b)); printf("sizeof(&d) = %d ,sizeof(&f) = %d ,sizeof(&g) = %d,sizeof(&h) = %d\n",sizeof(&d),sizeof(&f),sizeof(&g),sizeof(&h)); printf("sizeof(d) = %d ,sizeof(f) = %d ,sizeof(g) = %d,sizeof(h) = %d\n",sizeof(d),sizeof(f),sizeof(g),sizeof(h)); }

上面程序輸出的結果為:

sizeof(a) = 12,sizeof(b) = 8 ,sizeof(*b) = 12 sizeof(&d) = 8 ,sizeof(&f) = 8 ,sizeof(&g) = 8,sizeof(&h) = 8 sizeof(d) = 4 ,sizeof(f) = 1 ,sizeof(g) = 4,sizeof(h) = 8 在系統(tǒng)中 sizeof( char *)、sizeof( int *)、sizeof( float *)、sizeof( double *) 的長度均為8所以在sizeof時千萬要注意,使用不當會出現錯誤; 我的使用的是系統(tǒng)和編譯器為:[struggle@struggle alloc]$ cc -v 使用內建 specs。 COLLECT_GCC=cc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.5.1/lto-wrapper 目標:x86_64-redhat-linux 配置為:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,lto --enable-plugin --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux 線程模型:posix gcc 版本 4.5.1 20100924 (Red Hat 4.5.1-4) (GCC) [struggle@struggle alloc]$ uname -a Linux struggle 2.6.35.13-91.fc14.x86_64 #1 SMP Tue May 3 13:23:06 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux [struggle@struggle alloc]$

熱門評論
最新評論
發(fā)表評論 查看所有評論(0)
昵稱:
表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
字數: 0/500 (您的評論需要經過審核才能顯示)