id:minekoaさん経由。
ZDNetのC/C++のポインタ解説記事にて。
http://builder.japan.zdnet.com/sp/c-programming-language/story/0,3800083430,20370255,00.htm
http://builder.japan.zdnet.com/sp/c-programming-language/story/0,3800083430,20370255-2,00.htm
「これはひどい」としか言いようがない。ということで、サンプルコードをcygwinでコンパイル&実行してみたよ!
サンプルコードがコチラ。
#include int main( void ) { int *n; *n = 5; /* ポインタ変数nに値5を代入 */ printf( "%d\n", *n ); /* ポインタ変数nが持つ値(5)の出力 */ return 0; }
で、コンパイル&実行してみる。
sugarball@localhost ~ $ cat dame.c #include <stdio.h> int main( void ) { int *n; *n = 5; /* ポインタ変数nに値5を代入 */ printf( "%d\n", *n ); /* ポインタ変数nが持つ値(5)の出力 */ return 0; } sugarball@localhost ~ $ gcc dame.c sugarball@localhost ~ $ ./a.exe 8 [main] a 1680 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack) Segmentation fault (core dumped)
ハイ、core dumpですね。というのも、minekoaさん指摘のとおり、
「未初期化のポインタn の指す先に 値5 をカキコする」という大変危険なコードになっています。うあぁぁ。
というのが理由です。うあぁぁ。
せめて投稿前にコンパイルと実行くらいしてくれ。
閑話休題。
こんな実行できないコードを書く著者ですが、結構な数の書籍を出しているようです。
へー。