寫這篇原創文章是因為看到了極客中的一篇文章《有趣各種編程語言實現2+2=5》,其中C語言是這樣實現的:
int main() { char __func_version__[] = “5″; // For source control char b[]=”2″, a=2; printf(“%d + %s = %s\n”, a, b, a+b); return 0; }
我動手編譯了,結果不是5,確切的說是一個不可打印的ascii字符,所以console顯示的是:2+2= ,稍對C堆棧布局略有了解的都知道,其實這段代碼最後試圖打印的是__func_version__裡的字符串"5",但遺憾的是不同編譯器,甚至同一種編譯器用不通編譯選項生成得stack布局是截然不同的,這就無法保證精確定位b之後3字節正好指向__func_version__。
那麼在gcc -O3下到底布局如何呢?我們略微修改一下代碼:
#includeint main() { //char b[]="2", a=2; char __func_version__[] = "5"; // For source control char b[]="2", a=2; printf("%p %p %p\n",__func_version__,b,&a); /* for(int i=0;i<100;++i){ printf("%d + %s = %s\n", i, b, i+b); } */ printf("%d + %s = %s\n", a, b, a+b); return 0; }
我們來看一下結果:
gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc48/4.8.2/libexec/gcc/x86_64-apple-darwin13.0.0/4.8.2/lto-wrapper
Target: x86_64-apple-darwin13.0.0
Configured with: ../configure --build=x86_64-apple-darwin13.0.0 --prefix=/usr/local/Cellar/gcc48/4.8.2 --enable-languages=c,c++,objc,obj-c++ --program-suffix=-4.8 --with-gmp=/usr/local/opt/gmp4 --with-mpfr=/usr/local/opt/mpfr2 --with-mpc=/usr/local/opt/libmpc08 --with-cloog=/usr/local/opt/cloog018 --with-isl=/usr/local/opt/isl011 --with-system-zlib --enable-version-specific-runtime-libs --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --disable-werror --enable-plugin --disable-nls --disable-multilib
Thread model: posix
gcc version 4.8.2 (GCC)
cs$gcc -std=c99 -Wall -O3 -g0 -o 5 5.c
apple@kissAir: cs$./5
0x7fff504fa920 0x7fff504fa930 0x7fff504fa910
2 + 2 = OP?
#pragma pack(1) typedef struct __foo { char *b; char a; char *__func_version__; }foo; void print_5_by_struct(void) { foo foo_v = {"2",(char)2,"5"}; printf("%p %p\n",foo_v.__func_version__,foo_v.b); printf("%d + %s = %s\n",foo_v.a,foo_v.b,foo_v.a+foo_v.b); }
有些童鞋又會說了,你這樣結構太累贅鳥,太墨跡,不爽快!也好辦,沒說只能用gcc啊,我們試試clang吧 :)
#includeint main() { char __func_version__[] = "5"; // For source control char b[]="2", a=2; printf("%p %p %p\n",__func_version__,b,&a); printf("%d + %s = %s\n", a, b, a+b); return 0; }
clang -v
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.2.0
Thread model: posix
apple@kissAir: cs$clang -std=c99 -Wall -O3 -g0 -o 5 5.c
apple@kissAir: cs$./5
0x7fff57925936 0x7fff57925934 0x7fff57925933
2 + 2 = 5
所以說學C啥的光死看書不中啊,要學以致用啊,在此拋磚引玉,謝謝各位觀賞哦。