scanf 在读取文件时出现分段错误
scanf giving segmentation fault when reading in file
这个赋值要求我们只使用我们被告知要使用的特定变量。这意味着我们无法创建自己的任何一个。这是导致分段错误的代码:
int mem[100];
int *instructionCounter;
int *instructionRegister;
int *operationCode;
int *operand;
char str[20];
memset(str, 0 , 20);
scanf("%d %s %d" , instructionCounter, str, operand); //this is where the error occurs
我尝试使用 fgets 而不是 scanf 来读取字符串。我成功地读入了字符串,并尝试根据需要使用 sscanf 对其进行解析。但是,由于 int 指针实际上并不指向任何变量,因此我在那里也收到了分段错误。但就像我说的,除了上面列出的变量,我不允许创建任何其他变量。这就是我采用这种方法的原因。
我该怎么做才能避免这个分段错误?除了 scanf 之外,还有什么办法可以解决这个问题吗?感谢您的帮助。
C 是一种指针语言,在使用指针之前,请始终记住,您需要为指针分配内存区域,以确保它们引用进程虚拟内存地址中的有效内存地址 space.
因此,您的代码应该如下所示:
int mem[100]; // allocated in stack
int instructionCounter; // allocated in stack
int instructionRegister; // allocated in stack
int operationCode; // allocated in stack
int operand; // allocated in stack
char str[20]; // allocated in stack
memset(str, '[=10=]' , sizeof(str));
if (scanf("%d %s %d" , &instructionCounter, str, &operand) == 3)
…use the values…
else
…report erroneous input…
这是我在启用警告的情况下编译您的代码时得到的结果:
$ make CC=clang
clang -fsanitize=address -g -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -c -o testme.o testme.c
testme.c:15:24: warning: variable 'instructionCounter' is uninitialized when used here [-Wuninitialized]
scanf("%d %s %d" , instructionCounter, str, operand); //this is where the
^~~~~~~~~~~~~~~~~~
testme.c:9:28: note: initialize the variable 'instructionCounter' to silence this warning
int *instructionCounter;
^
= NULL
testme.c:15:49: warning: variable 'operand' is uninitialized when used here [-Wuninitialized]
scanf("%d %s %d" , instructionCounter, str, operand); //this is where the
^~~~~~~
testme.c:12:17: note: initialize the variable 'operand' to silence this warning
int *operand;
^
= NULL
2 warnings generated.
clang -fsanitize=address testme.o -o testme
请注意,编译器不希望您使用这些未初始化的变量,但它的解决方案解决了这个问题,但没有解决它以外的问题。您还必须分配这些变量。
试试这个:
int instructionCounter;
int operand;
char str[20];
memset(str, 0 , 20);
scanf("%d %s %d" , &instructionCounter, str, &operand);
这个赋值要求我们只使用我们被告知要使用的特定变量。这意味着我们无法创建自己的任何一个。这是导致分段错误的代码:
int mem[100];
int *instructionCounter;
int *instructionRegister;
int *operationCode;
int *operand;
char str[20];
memset(str, 0 , 20);
scanf("%d %s %d" , instructionCounter, str, operand); //this is where the error occurs
我尝试使用 fgets 而不是 scanf 来读取字符串。我成功地读入了字符串,并尝试根据需要使用 sscanf 对其进行解析。但是,由于 int 指针实际上并不指向任何变量,因此我在那里也收到了分段错误。但就像我说的,除了上面列出的变量,我不允许创建任何其他变量。这就是我采用这种方法的原因。
我该怎么做才能避免这个分段错误?除了 scanf 之外,还有什么办法可以解决这个问题吗?感谢您的帮助。
C 是一种指针语言,在使用指针之前,请始终记住,您需要为指针分配内存区域,以确保它们引用进程虚拟内存地址中的有效内存地址 space.
因此,您的代码应该如下所示:
int mem[100]; // allocated in stack
int instructionCounter; // allocated in stack
int instructionRegister; // allocated in stack
int operationCode; // allocated in stack
int operand; // allocated in stack
char str[20]; // allocated in stack
memset(str, '[=10=]' , sizeof(str));
if (scanf("%d %s %d" , &instructionCounter, str, &operand) == 3)
…use the values…
else
…report erroneous input…
这是我在启用警告的情况下编译您的代码时得到的结果:
$ make CC=clang
clang -fsanitize=address -g -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -c -o testme.o testme.c
testme.c:15:24: warning: variable 'instructionCounter' is uninitialized when used here [-Wuninitialized]
scanf("%d %s %d" , instructionCounter, str, operand); //this is where the
^~~~~~~~~~~~~~~~~~
testme.c:9:28: note: initialize the variable 'instructionCounter' to silence this warning
int *instructionCounter;
^
= NULL
testme.c:15:49: warning: variable 'operand' is uninitialized when used here [-Wuninitialized]
scanf("%d %s %d" , instructionCounter, str, operand); //this is where the
^~~~~~~
testme.c:12:17: note: initialize the variable 'operand' to silence this warning
int *operand;
^
= NULL
2 warnings generated.
clang -fsanitize=address testme.o -o testme
请注意,编译器不希望您使用这些未初始化的变量,但它的解决方案解决了这个问题,但没有解决它以外的问题。您还必须分配这些变量。
试试这个:
int instructionCounter;
int operand;
char str[20];
memset(str, 0 , 20);
scanf("%d %s %d" , &instructionCounter, str, &operand);