2012-04-21 5 views
1

몇 가지 segfault 예제를 시도하고 있지만 그 중 아무 것도 오류의 원인이 아닙니다.세그먼트 오류 세그먼트 오류를 ​​제공하지 않는 예

소스 코드에서 : http://www.unknownroad.com/rtfm/gdbtut/gdbsegfault.html

예 않습니다 :

prompt> gcc -g segfault.c 

이제 우리는 프로그램을 실행 :

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 > ./a.out 
Hello World! 
Segmentation fault 
prompt > 

그러나 위 예제는 내 우분투에서 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) 

내 GCC는 자동으로 초기화 변수 :

내 GCC의 출력입니다. 나는 그것을 해제하고 싶다.

비슷한 문제가 있거나 솔루션에 대해 알고있는 사람이 있습니까?

+1

당신은'시도해야 수/a.out'? (try'which a.out') – wildplasser

+0

나는 ./a.out을 실행 중이다. 코드가 완벽하게 작동합니다. – user1348438

+2

당신의 예제는 메모리 할당 실패로 인한'segfault'에 달려 있습니다. 'buf'가'malloc' 다음에 오는 지 확인하십시오. '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'.