简体   繁体   中英

scanf giving segmentation fault when reading in file

This assignment requires that we only use specific variables that we are told to use. That means we cannot create any of our own. Here's the code that is causing the segmentation fault:

    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

I tried using fgets instead of scanf to read in the string. I read in the string successfully, and I tried to use sscanf to parse it as needed. However, since the int pointers don't actually point to any variable, i received a segmentation fault there too. But like I said, I'm not allowed to create any other variables than ones listed above. That is why I took this approach.

What can I do to avoid this segmentation fault error? Is there any way other than scanf that I can solve this? Thanks for your help.

C is a pointer language and before playing with pointers always keep in mind that, you need an allocated memory area for your pointers to make sure that they're refering a valid memory address in your process' virtual memory address space.

Therefore, your code should have been like the following:

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, '\0' , sizeof(str));
if (scanf("%d %s %d" , &instructionCounter, str, &operand) == 3)
    …use the values…
else
    …report erroneous input…

Here's what I get when I compile your code with warnings enabled:

$ 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

Note that the compiler doesn't want you to use these uninitialized variables but its solution solves that problem but not the one beyond it. You must also allocate these variables.

Try this instead:

int instructionCounter;
int operand;
char str[20];
memset(str, 0 , 20);
scanf("%d %s %d" , &instructionCounter, str, &operand);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM