Home > Enterprise >  How to stop segmentation fault (core dumped)?
How to stop segmentation fault (core dumped)?

Time:11-25

I am trying to learn c, but everytime i run a code that needs user imput i get segfault error My os is arch linux it was compiled with "gcc -o test1 test1.c" Appearently it happens because the program cannot allocate memory, but none of the tutorials i saw did any extra thing.

here is the code i was trying to run:

   #include<stdio.h>
     int main(){
           int age;
           scanf("%d", age);
           printf("age = %d", age);
           return 0;
   }

and when i run it with ./test1 i get 19315 segmentation fault (core dumped) ./test I tried looking it up on google and found nothing that solved this

CodePudding user response:

scanf() expects its arguments (except the first one, which is the format string) to point at the memory where values are to be stored. It is trying to store the user input into whatever memory age points at when interpreted as a pointer, and it doesn't have permission to write there, so it segfaults. To fix the segfault, add the address-of operator in front of age in the scanf() invocation:

scanf("%d", &age);

This way scanf will store the integer into age instead of writing into whatever address it thinks is in age.

CodePudding user response:

Nathan is correct about the fix (scanf("%d", &age)), but it could be useful to go into a bit more detail as to exactly what's happening here.

Segfaults don't happen when the program can't allocate memory (and you should avoid whatever tutorial told you otherwise). Rather, they happen when your program tries to access memory that it doesn't have permission (from the operating system) to access. (1) (This keeps a normal user's programs from reading confidential information from other users' programs, among other things.)

So what was happening in your code was the following:

  1. int age; declares an int named age and claims some space on the stack for it to go. You don't initialize it, so age contains whatever happens to live in that memory already. Let's assume it's 42.
  2. scanf("%d", age); tries to read an int from stdin, then put it at the location pointed to by age. age isn't actually a pointer, but pointers are (to first approximation) just numbers representing an address, so scanf gleefully attempts to write to address 42. (gcc really should have warned you about this; I'm not sure why it didn't.)
  3. Your program doesn't have permission to access address 42, so you get the segfault.

(1) There are a few ways to get a segfault as an indirect result of running out of memory: if you declare a gigantic array as a local variable, it can overflow the stack (the memory allocated for locals) and try to read whatever's on the other end; that might lead to a segfault depending on what's there. And if you malloc some memory, malloc fails (and returns NULL), and you don't check the return code, then trying to read the NULL you got from malloc will generally cause a segfault. But it's always the access attempt that actually causes the segfault.

  • Related