Segmentation fault

Discussion in 'C Programming' started by Shuaib, Jul 22, 2006.

  1. Shuaib

    Shuaib Guest

    The following simple code gives an error of: "Segmentation fault" just
    as I press Enter after entering a word. Any idea why this might be
    occuring?

    I am using GCC to compile the programe. Here is an output of $gcc
    --version

    gcc (GCC) 3.3.5-20050130 (Gentoo 3.3.5.20050130-r1,
    ssp-3.3.5.20050130-1, pie-8.7.7.1)
    Copyright (C) 2003 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.

    =========CODE=========

    #include <stdio.h>
    int main()
    {
    printf("Enter a word: ");
    char word[20];
    scanf("%s", word);
    printf("%s", word);
    FILE *file;
    file=fopen("file1", "r+");
    fclose(file);

    return 0;
    }

    =========CODE==========
    Shuaib, Jul 22, 2006
    #1
    1. Advertising

  2. Shuaib

    Tom St Denis Guest

    Shuaib wrote:
    > #include <stdio.h>
    > int main()
    > {
    > printf("Enter a word: ");
    > char word[20];
    > scanf("%s", word);
    > printf("%s", word);
    > FILE *file;
    > file=fopen("file1", "r+");
    > fclose(file);
    >
    > return 0;
    > }


    Do this

    gcc -g3 myfile.c -o myfile
    gdb myfile

    Then type "run" and hit enter

    Tom
    Tom St Denis, Jul 22, 2006
    #2
    1. Advertising

  3. Shuaib

    Guest

    Shuaib wrote:

    > The following simple code gives an error of: "Segmentation fault" just
    > as I press Enter after entering a word. Any idea why this might be
    > occuring?
    >
    > I am using GCC to compile the programe. Here is an output of $gcc
    > --version
    >
    > gcc (GCC) 3.3.5-20050130 (Gentoo 3.3.5.20050130-r1,
    > ssp-3.3.5.20050130-1, pie-8.7.7.1)
    > Copyright (C) 2003 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.
    >
    > =========CODE=========
    >
    > #include <stdio.h>
    > int main()
    > {
    > printf("Enter a word: ");
    > char word[20];
    > scanf("%s", word);
    > printf("%s", word);
    > FILE *file;
    > file=fopen("file1", "r+");
    > fclose(file);
    >
    > return 0;
    > }


    It could be that the word you enter through the keyboard is
    longer than 20 characters. Or that file1 does not exist. You
    should modify your code so that it checks that fopen does not
    return NULL.

    Spiros Bousbouras
    , Jul 22, 2006
    #3
  4. Shuaib

    Shuaib Guest

    Thanks both of the above. I got the solution now.

    GDB showed that there was something wrong with fclose(). Chaging the
    opening mode from r+ to w+ in fopen() did the trick. I guess the file
    didn't exist and we needed to creat it, which the w+ handles, not the
    r+. :)

    Thanks again.
    Shuaib, Jul 22, 2006
    #4
  5. Shuaib

    Shuaib Guest

    BTW, the thing that really bugs me is that, it gives "Segmentation
    fault" error just as I press enter after the word (which is less than
    20 chars :) ). At least it should go on with printing the word, and
    only when coming by the fclose() should it give the error. Isn't that
    how it is suppose to work?
    Shuaib, Jul 22, 2006
    #5
  6. Shuaib

    Tom St Denis Guest

    Shuaib wrote:
    > Thanks both of the above. I got the solution now.
    >
    > GDB showed that there was something wrong with fclose(). Chaging the
    > opening mode from r+ to w+ in fopen() did the trick. I guess the file
    > didn't exist and we needed to creat it, which the w+ handles, not the
    > r+. :)


    I'm glad you actually used gdb. Good tool to learn [hint: use it!!!]

    Note your problem isn't that you used the wrong mode. Your problem is
    you didn't check the return of fopen(). Just because you used "w+"
    doesn't mean it will return a successfully opened file. You could have
    a full disk, lack permissions, etc, etc.

    The real solution is to always check the return [this goes for the
    *alloc() series of functions too].

    Tom
    Tom St Denis, Jul 22, 2006
    #6
  7. In article <>,
    Shuaib <> wrote:
    >
    >BTW, the thing that really bugs me is that, it gives "Segmentation
    >fault" error just as I press enter after the word (which is less than
    >20 chars :) ). At least it should go on with printing the word, and
    >only when coming by the fclose() should it give the error. Isn't that
    >how it is suppose to work?


    The real, Unix-specific answer is: stdout is line buffered.

    I will leave it to the pedants to come up with the religiously-correct
    clc answer.

    Useful clc-related links:

    http://en.wikipedia.org/wiki/Clique
    http://en.wikipedia.org/wiki/Aspergers
    http://en.wikipedia.org/wiki/C_programming_language
    Kenny McCormack, Jul 22, 2006
    #7
  8. Shuaib

    Tom St Denis Guest

    Kenny McCormack wrote:
    > >BTW, the thing that really bugs me is that, it gives "Segmentation
    > >fault" error just as I press enter after the word (which is less than
    > >20 chars :) ). At least it should go on with printing the word, and
    > >only when coming by the fclose() should it give the error. Isn't that
    > >how it is suppose to work?

    >
    > The real, Unix-specific answer is: stdout is line buffered.
    >
    > I will leave it to the pedants to come up with the religiously-correct
    > clc answer.


    I'm gonna give you some advice my momma gave me when I was a wee lad.

    "If yee ain't got nothing proper to say, then shut yer pie hole."

    Seriously, drop the attitude. Either answer the fucking question
    properly, or shut the **** up.

    And the answer IS because the file stream isn't guaranteed to be
    flushed until you either close it or send a newline. Not exactly
    rocket science.

    Tom
    Tom St Denis, Jul 22, 2006
    #8
  9. "Shuaib" <> writes:
    > BTW, the thing that really bugs me is that, it gives "Segmentation
    > fault" error just as I press enter after the word (which is less than
    > 20 chars :) ). At least it should go on with printing the word, and
    > only when coming by the fclose() should it give the error. Isn't that
    > how it is suppose to work?


    Please quote context. Google now makes this easy to do, but read
    <http://cfaj.freeshell.org/google/> anyway; see also
    <http://www.caliburn.nl/topposting.html>.

    You printed a string without a trailing newline. The system probably
    buffered the string and waited for a newline before printing it
    (stdout is typically line-buffered). Since the program blew up before
    it had a chance to finish, the buffered output was lost.

    Changing this:
    printf("%s", word);
    to this:
    printf("%s\n", word);
    would probably cause the output to appear before the seg fault. If
    you don't want the newline for some reason, you can also call
    fflush(stdout).

    One more thing: your program mixes declarations and statements. This
    is allowed by C99, and as an extension by some pre-C99 compilers, but
    it's not permitted by the C90 standard. If you want your code to be
    portable, put all your declarations at the beginning of the block,
    followed by all your statements.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jul 22, 2006
    #9
  10. Shuaib

    Shuaib Guest

    Phew!

    Good material Tom, and Keith. Thanks alot! I got an understanding of
    the problem now.
    Shuaib, Jul 22, 2006
    #10
  11. In article <>,
    Tom St Denis <> wrote:
    ....
    >> I will leave it to the pedants to come up with the religiously-correct
    >> clc answer.

    ....
    >And the answer IS because the file stream isn't guaranteed to be
    >flushed until you either close it or send a newline.


    Thank you for not disappointing me. I knew I could count on you.
    Kenny McCormack, Jul 22, 2006
    #11
  12. Shuaib

    jacob navia Guest

    Tom St Denis a écrit :
    >
    > I'm gonna give you some advice my momma gave me when I was a wee lad.
    >
    > "If yee ain't got nothing proper to say, then shut yer pie hole."
    >


    Ahhh good advice was that. GOOD ADVICE.

    Why don't you remember it more often?
    jacob navia, Jul 22, 2006
    #12
  13. On 22 Jul 2006 11:35:05 -0700, "Shuaib" <>
    wrote:

    snip

    >#include <stdio.h>
    >int main()
    >{
    > printf("Enter a word: ");
    > char word[20];


    Unless you have a C99 compiler, definitions should precede statements.
    That way, people whose compilers do check will also be able to help
    you.

    > scanf("%s", word);
    > printf("%s", word);
    > FILE *file;
    > file=fopen("file1", "r+");
    > fclose(file);
    >
    > return 0;
    >}



    Remove del for email
    Barry Schwarz, Jul 22, 2006
    #13
  14. Shuaib <> wrote:

    > The following simple code gives an error of: "Segmentation fault" just
    > as I press Enter after entering a word. Any idea why this might be
    > occuring?


    That's already been answered, but I'm surprised no one has yet made
    this observation:

    > char word[20];
    > scanf("%s", word);


    These lines are absolutely no better than using gets(), a function
    which should never be used. Be safe and specify a maximum length:

    scanf("%19s",word); /* Leave space for '\0' at end */

    Better yet, avoid all the perils of scanf() and use fgets().

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Jul 25, 2006
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley
    Replies:
    17
    Views:
    849
  2. Pud
    Replies:
    0
    Views:
    563
  3. Replies:
    0
    Views:
    515
  4. Ivan Vecerina
    Replies:
    0
    Views:
    476
    Ivan Vecerina
    Jun 29, 2003
  5. Vasileios Zografos

    Re: segmentation fault exception handling

    Vasileios Zografos, Jun 30, 2003, in forum: C++
    Replies:
    5
    Views:
    15,573
    Pete Becker
    Jul 1, 2003
Loading...

Share This Page