Getting a segmentation fault

This is a discussion on Getting a segmentation fault within the C Programming forums, part of the General Programming Boards category; Hello everyone, I have just started programming in Linux.I tried this basic program : Code: #include<stdio.h> int main() { char ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    31

    Getting a segmentation fault

    Hello everyone,
    I have just started programming in Linux.I tried this basic program :

    Code:
    #include<stdio.h>
    
    int main()
    {
    	char *buffer;
    	scanf("%s",buffer);
    	printf("\n%s\n",buffer);
    	return 0;
    }
    This gives a segmentation fault in Linux but it runs perfectly on Borland C++ in Windows.
    How can I avoid this problem?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    buffer needs to be allocated some space.
    Like
    char buffer[100];

    > This gives a segmentation fault in Linux but it runs perfectly on Borland C++ in Windows.
    That's just dumb luck. Sometimes code just works despite all attempts to make a mess of it.

    But just because it seems to produce the correct answer doesn't make the code correct.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    31
    On using char buffer[100],I am limited to only 100 characters.Suppose I have a string of say 1000 characters it causes an overflow.Though,I can use malloc() I need to know the number of bytes to be allocated to the string.Suppose I don't know this value what should I do?
    What exactly I am trying to do is accept a string of unknown length i.e the length is known only after the user enters the string.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    You can't input a string of unknown length directly. You have to read it in in fixed sized blocks (of known size), then keep extending the space allocated until you reach whatever it is that terminates the input.

    In other words, fget() + malloc() + a loop of some sort.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,458
    Quote Originally Posted by Salem View Post
    In other words, fget() + malloc() + a loop of some sort.
    fgets...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by vart View Post
    fgets...
    fgets() is not as simple as it looks.

    If the line is longer than the buffer size, then the result will be truncated. The only way to detect this is to check if the final character in the buffer is '\n'. If it's not, you still are not sure whether a truncation occurred, because it could merely be that the last line in the file did not have a newline at the end of it. So if there is no trailing '\n' you additionally have to check feof().

    When you code all this up, you actually end up with something that looks pretty terrible, whereas the simpler implementation based on fgetc() and dynamically resizing the buffer is actually a lot easier to understand.

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    fgets() is not as simple as it looks.

    If the line is longer than the buffer size, then the result will be truncated. The only way to detect this is to check if the final character in the buffer is '\n'. If it's not, you still are not sure whether a truncation occurred, because it could merely be that the last line in the file did not have a newline at the end of it. So if there is no trailing '\n' you additionally have to check feof().

    When you code all this up, you actually end up with something that looks pretty terrible, whereas the simpler implementation based on fgetc() and dynamically resizing the buffer is actually a lot easier to understand.
    Thats something I had never realised. However considering that the OP is reading in data from the keyboard I would have thought that the input would be guaranteed to to end in a newline, which would mean means that in this case it would be possible to check for truncation.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by mike_g View Post
    Thats something I had never realised. However considering that the OP is reading in data from the keyboard I would have thought that the input would be guaranteed to to end in a newline, which would mean means that in this case it would be possible to check for truncation.
    In theory, stdin could have been redirected from some other source, and could contain a final line with no trailing newline.

    fgets() has other dangers, such as if the input contains '\0'. If you think about it, you realize that there is actually no way to determine how many characters were read by fgets(). And therefore, no way to actually check the last character to see if it's '\n' (because you have no way of knowing where the last character is, if '\0' is thrown into the mix).

    Writing robust line getting functions is stupid-hard. The GNU C library has a non-standard function called getline() which does it for you. I was kind of shocked that it didn't make it into C99. So we're still left rolling our own implementations of a basic function that should have been part of the standard to begin with. It sucks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

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