2012-04-21 74 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'.