2012-04-21 152 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 -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) 

我的海灣合作委員會也將自動初始化變量。我想禁用它。

有沒有人遇到類似的問題,或者對解決方案有所瞭解?

+1

願你應該試着去'/ a.out'? (嘗試'哪個a.out') – wildplasser 2012-04-21 15:28:22

+0

我正在運行./a.out。代碼工作完美無瑕。 – user1348438 2012-04-21 15:45:28

+2

您的示例取決於由於未能分配內存導致的'segfault'ing。在'malloc'後面用'buf'指向。 'printf(「%p \ n」,buf);'malloc'後面的輸出是什麼? – 2012-04-21 17:12:39

回答

2

示例代碼只有segfaults,如果malloc(1<<31)失敗,則返回NULL。但是,如果您的系統能夠分配2個內存而不發生故障,則malloc(1<<31)成功。該段錯誤示例代碼片段來自系統通常不能分配該內存量的時間。是否成功取決於物理內存的數量,其他進程佔用的內存量,內核的內存過度使用策略,以及可能使用的(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'.