Thread: Surprising Segfault

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    18

    Surprising Segfault

    Hi,

    I've the following code in main() function.. The scanf is written to read multiple strings in a single array of char:
    -----
    --
    Code:
    else
                {
                    printf("\n\n MODULAR VIEW \n");
                    printf("cmd> Type help to get help \n");
                    printf("cmd>");                
                    scanf("%[^\n]",usr_in);          
                    help_module(usr_in);
                }
    The prototype of the called function is:
    Code:
    void help_module(char *);
    The variable declaration is:
    Code:
    char user_in[80]
    This segfaults during execution.:

    0 -> Traditional View
    Other -> Modular View
    Enter your choice :1


    MODULAR VIEW
    cmd> Type help to get help
    Segmentation fault

    The surprising point here is that - its getting segfault before scanf. Means its just printing the printf statements and then segfault. Why so? I've checked by putting some more printf's before the function call and scanf. But anytime its not reading any value through scanf. Simply segfaults. What may be the reason here?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    When is a segfault not a surprise

    Anyway, instead of posting random lines of code expecting us to guess what you did wrong, run the code in the debugger and it will stop you at the precise point of the segfault.

    Then you can start looking around the current code state to see which pointer is NULL or garbage.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    18
    Compiled with "-g" option:

    Code:
     icc -g lsub.c -o lsubg
    This is the output of gdb:

    Code:
    (gdb) file lsubg
    Reading symbols from /windows/D/san/work/work/lsubC/lsubg...(no debugging symbols found)...done.
    (gdb) run
    Starting program: /windows/D/san/work/work/lsubC/lsubg
    Detaching after fork from child process 17905.
    Detaching after fork from child process 17906.
    
    
    0 -> Traditional View 
     Other -> Modular View 
     Enter your choice :1
    
    
     MODULAR VIEW 
    cmd> Type help to get help 
    cmd>
    cmd again 
    
    Program received signal SIGSEGV, Segmentation fault.
    0x000000396e67efa2 in strcmp () from /lib64/libc.so.6
    Missing separate debuginfos, use: debuginfo-install glibc-2.10.1-5.x86_64 libgcc-4.4.1-2.fc11.x86_64
    (gdb)
    But if I change the scanf statement from
    Code:
    scanf("%[^\n]",usr_in);
    to
    Code:
    scanf("%s", usr_in);
    It runs well. But it stores only the first word. Any hints why this scanf giving segfault

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The faulting function call is strcmp, not scanf

    The first thing you do is 'bt' to get the stack trace showing where you called strcmp from.
    Examine in detail the parameters you're passing - one of them isn't a valid address.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    18
    Quote Originally Posted by Salem View Post
    The faulting function call is strcmp, not scanf

    The first thing you do is 'bt' to get the stack trace showing where you called strcmp from.
    Examine in detail the parameters you're passing - one of them isn't a valid address.
    The strcmp function is used in the following way:

    The char array contains set of strings.

    Code:
    char*  help_str[] = {"help","help one","help one two", "help one two three", "help one two three four"};
    And there is a string which is inputted from user: user_val (Declared as char user_val[50]). This string is passed to the user-defined function, in which the strcmp operation is perfomed against the items present in the array help_str. In this step, its giving seg-fault.

    Code:
    strcmp(usr_val,help_str[j]);

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    And the loop that is counting j is what?

    If you step off the end of that array, it will fault.

    You should post more code, rather than just single lines.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    In my house
    Posts
    32
    Quote Originally Posted by sangamesh View Post

    But if I change the scanf statement from
    Code:
    scanf("%[^\n]",usr_in);
    to
    Code:
    scanf("%s", usr_in);
    It runs well. But it stores only the first word. Any hints why this scanf giving segfault
    Just a tip, scanf separates strings when it encounters a space or newline (and maybe something else, correct me if I'm wrong)

    typing in:
    Code:
    char Variable1[80], Variable2[80], Variable3[80];
    
    scanf("%s", Variable1);
    scanf("%s%s", Variable2, Variable3);
    
    printf("%s\n%s%s", Variable1, Variable2, Variable3);
    (if we type in "Hello World" into the console) it would return
    Code:
    Hello
    Hello World!
    Scanf sees a space and ends off the variable there, thats why it would require 2 variables to actually take in "Hello World!"

    To get around this (and a safe way to do so) is to use fgets. An example of this would be something such as:

    Code:
    char Variable[80];
    fgets(Variable, sizeof Variable, stdin);
    Its not only faster than scanf (atleast thats what I heard because scanf has to check if its taking in an int,double,float,string, and formatting) its also safer because you're limiting how much data can be taken in (that "sizeof Variable") which will make sure the buffer doesn't overflow. Correct me if I'm wrong with any of this, its all of the top of my head.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault
    By Triumph in forum C Programming
    Replies: 5
    Last Post: 04-10-2009, 02:24 AM
  2. Sometimes segfault, sometimes not
    By jcafaro10 in forum C Programming
    Replies: 18
    Last Post: 04-07-2009, 06:53 PM
  3. Segfault
    By oddball in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 07:53 AM
  4. segfault with gcc, but not with TC
    By koodoo in forum C Programming
    Replies: 15
    Last Post: 04-23-2007, 09:08 AM
  5. Startling, but not surprising news report
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 12-10-2004, 05:08 PM