2012-04-21 13 views
1

いくつかのsegfaultの例を試していますが、どれもエラーを引き起こしていません。セグメンテーションフォルトセグメンテーションフォールトを与えない例

からソースコード:http://www.unknownroad.com/rtfm/gdbtut/gdbsegfault.html

例は以下の通りです:

1 : #include <stdio.h> 
2 : #include <stdlib.h> 

3 : int main(int argc, char **argv) 
4 : { 
5 : char *buf; 
6 : 
7 : buf = malloc(1<<31); 
8 : 
9 : fgets(buf, 1024, stdin); 
10: printf("%s\n", buf); 
11: 
12: return 1; 
13: } 

最初のステップは、デバッグフラグを使用してプログラムをコンパイルすることです:

prompt> gcc -g segfault.c 

今、私たちは、プログラムを実行します:

prompt > ./a.out 
Hello World! 
Segmentation fault 
prompt > 

ただし、上の例は私のUbuntuでsegfaultなしで実行しています。私はgccオプションに関連するものだと思っていますが、これを引き起こしているものは見つけられませんでした。別のディストリビューションで実行すると、問題が発生します。

これは私のgccの出力です:

$ gcc -v 
Using built-in specs. 
Target: i486-linux-gnu 
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5.1' 
    --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs 
    --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared 
    --enable-multiarch --enable-linker-build-id --with-system-zlib 
    --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix 
    --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls 
    --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc 
    --enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic 
    --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu 
    --target=i486-linux-gnu 
Thread model: posix 
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1) 

私のgccが自動的に変数を初期化しています。私はそれを無効にしたい。

誰も同じような問題に直面したのですか、解決策についてご意見がありましたか?

+1

は '/ a.out'を試してみてくださいことはできますか? (try 'which a.out') – wildplasser

+0

私は./a.outを実行しています。コードは完璧に動作しています。 – user1348438

+2

あなたの例は、メモリ割り当てに失敗したために 'segfault'に依存しています。 'malloc'の後に' buf'を指す点でチェックしてください。 'printf("%p \ n "、buf);' 'malloc'の直後の出力はどうですか? –

答えて

2

malloc(1<<31)が失敗した場合、サンプルコードはsegfaultsのみNULLを返します。しかし、malloc(1<<31)は、システムが2ギガバイトのメモリを問題なく割り当てできる場合に成功します。そのsegfaultサンプルのコードスニペットは、システムが通常その量のメモリを割り当てることができなかった時からのものです。それが成功するかどうかは、物理メモリの量、他のプロセスによって使用されたメモリの量、およびカーネルのメモリオーバーコミット戦略に加えて、おそらく使用される(libcと)カーネルのバージョンによって決まります。したがって、異なる動作は、オーバーコミットsysctl、実行中の異なるプロセスセット、または明らかに物理メモリの量が異なることの異なる設定の結果である可能性があります。 /usr/src/linux/Documentation/vm/overcommit-accountingから

:。

The Linux kernel supports the following overcommit handling modes 

0 - Heuristic overcommit handling. Obvious overcommits of 
     address space are refused. Used for a typical system. It 
     ensures a seriously wild allocation fails while allowing 
     overcommit to reduce swap usage. root is allowed to 
     allocate slightly more memory in this mode. This is the 
     default. 

1 - Always overcommit. Appropriate for some scientific 
     applications. 

2 - Don't overcommit. The total address space commit 
     for the system is not permitted to exceed swap + a 
     configurable percentage (default is 50) of physical RAM. 
     Depending on the percentage you use, in most situations 
     this means a process will not be killed while accessing 
     pages but will receive errors on memory allocation as 
     appropriate. 

The overcommit policy is set via the sysctl `vm.overcommit_memory'. 

The overcommit percentage is set via `vm.overcommit_ratio'. 
関連する問題