내 코드가 segfaulting이고 무엇이 잘못되었는지 전혀 모른다. 가능한 한 간소화했지만 문제는 찾을 수 없습니다.C - fclose() 호출로 코어 덤프
C 파일 TEST.C :
$ gcc --version
gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gcc -Wall -g test.c && gdb a.out
GNU gdb (GDB) 7.6.1-ubuntu
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/dberg/ITX/Cells/test/a.out...done.
(gdb) break 26
Breakpoint 1 at 0x400739: file test.c, line 26.
(gdb) run test.txt
Starting program: /home/dberg/ITX/Cells/test/a.out test.txt
Breakpoint 1, main (argc=2, argv=0x7fffffffdf48) at test.c:26
26 fclose(fh);
(gdb) n
Program received signal SIGSEGV, Segmentation fault.
__GI___libc_free (mem=0x1) at malloc.c:2892
2892 malloc.c: No such file or directory.
(gdb)
되지 않는 구조체 멤버 중 하나를 삭제 : 실행하고 gdb를 통해 실행
#include <stdlib.h>
#include <stdio.h>
struct container {
void *A[3], *B[3], *C[3], *D[3];
int x, y, z;
};
int main (int argc, char* argv[]) {
struct container *cont = malloc (sizeof cont);
FILE* fh = fopen(argv[1], "r");
if(fh == NULL) return 0;
fscanf(fh, "%d %d", &cont->y, &cont->z);
fclose(fh);
free(cont);
return 0;
}
있는 test.txt의 내용
1 1
코드를 오류없이 실행할 수 있습니다. 사용되지 않는 구조체 멤버 중 하나를 구조체의 끝으로 이동하거나 1 또는 모든 배열의 크기를 줄이면 코드가 성공적으로 실행될 수 있습니다. segfault에 fscanf() 호출이 필요합니다.
구문이 잘못되었으며 구조체의 크기가이 버그에 너무 중요한 이유는 무엇입니까?
힌트 : 'sizeof cont'란 무엇입니까? – Mysticial
표제 : 멋지게 형성된 질문입니다. – chux