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 在系统中 sizeof( char *)、sizeof( int *)、sizeof( float *)、sizeof( double *) 的长度均为8所以在sizeof时千万要注意,使用不当会出现错误; 我的使用的是系统和编译器为:[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]$