Segmantation Fault (core dumped) error

This is a discussion on Segmantation Fault (core dumped) error within the C Programming forums, part of the General Programming Boards category; this is my first post. I am new to C programming (student < 3 months). While doing an assignment I ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    Unhappy Segmantation Fault (core dumped) error

    this is my first post. I am new to C programming (student < 3 months). While doing an assignment I keep getting an error of Segmantation Fault (core dumped) when I run my program. I have posted the section that I get this error on (the if statements work, and it only does one of the three choices... I get the same error no matter what I enter into the buffer ):

    int getLine (string buffer)
    {
    string inputString;
    int x;

    printf("input string ===>");
    inputString = gets(buffer);

    if (*buffer == NULL || *buffer == EOF)
    {
    x = FALSE;
    printf("buffer = %s \n",buffer); /* Here .. it prints the buffer then I get the error /*
    printf("Value of x = %i",x); /*it does not print this */
    return x;
    }
    if (strcmp (buffer,"*END*") == 0)
    {
    x = FALSE;
    printf("buffer = %s \n",buffer); /* Here .. it prints the buffer then I get the error /*

    printf("Value of x = %i",x); /*it does not print this */
    return x;
    }

    x = TRUE;
    printf("buffer = %s \n",buffer); /* Here .. it prints the buffer then I get the error /*

    printf("Value of x = %i",x); /*it does not print this */
    return x;
    }
    /* End of the getLine function */

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    Thanks for the help

    vVv,
    Thanks for the help !
    The EOF is in there as part of the assignment. I have done some more debugging (adding in some printf statements) and have narrowed it down to another section of my code. I am working on it now that I know where the problem is.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >While doing an assignment I keep getting an error of Segmantation Fault
    The problem is most likely your attempt to copy a string into an area of memory that can't hold it, you may be passing a char * instead of an array or you neglected to allocate memory for the char *.

    >inputString = gets(buffer);
    This is where you get burned, first gets won't check boundaries so it's very unsafe. Second, buffer apparently isn't large enough for whatever you're entering.

    >if (*buffer == NULL || *buffer == EOF)
    This won't work, lose the dereferencing on buffer and buffer == NULL will work, but buffer == EOF will give you a warning because buffer is a pointer to char and EOF is an int value.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    Thanks again

    Prelude,

    Thank you for the helpful pointers !
    I saw in a couple of post that I should be using fgets instead of gets. I will investigate this option.

    The buffer size is 1024 for the buffer that I am trying to pass into this function.

    My teacher has really jumped from some simple concepts like doing some functions with integers (ie. circle radius,area) to some heavy programming with passing multiple arrays, and working with pointers in a program that has 7 functions.

    Thanks again for your help !

  5. #5
    Registered User Dave Jay's Avatar
    Join Date
    Mar 2002
    Posts
    33

    if compatibility is not a concern

    check to see if your compiler has cgets(); (DOS) in conio.h

    What's nice about this function is that you can specify a max input length so the user can't overrun an input string into another window() defined area.

  6. #6
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Besides using print statements you should learn how to use gdb.
    Just compile your file with gcc -g proj.c -o proj
    and then run
    gdb ./proj and then type in run
    Once it segfaults you can get a stack trace with typing in back.
    As long as you havn't screwed up the function call stack it should give you valid results.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    4

    Smile everything is working !

    I got my program to work !!

    Thanks for all the suggestions, and help guys !!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 05:02 PM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 11:55 PM
  4. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 12:54 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21