Thread: gcc and gets

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    19

    gcc and gets

    hey.
    when I use gcc to compile my .c-files it complains about the gets library function and says that "it is dangerous and should not be used". anyone know why?

    dagH

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Read the FAQ regarding gets() for a full explaination...

    But anyway, by using gets() you risk having characters left in the input stream (stdin), usually new lines after you've taken what you've asked for out. gets() can be used, providing you know the risks behind it... You also run the risk of overflowing the buffer you specify, since gets() doesn't ask how bit it is (it just assumes it can hold all the input).

    You can use fgets() instead so you can avoid buffer over-runs and new-line issues.
    Last edited by zacs7; 04-29-2007 at 05:29 AM.

  3. #3
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    anyone know why?
    Because it's dangerous and shouldn't be used? gets() doesn't allow you to stop reading when the array is full, so can end up writing past the end if the array and breaking all kinds of things. Use fgets() instead because it lets you pass a limit to the number of characters it can read.
    by using gets() you risk having characters left in the input stream (stdin), usually new lines after you've taken what you've asked for out.
    gets() doesn't have that problem at all. It always reads a full line, including the '\n'.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No it doesn't - the trailing \n is left in the stream,

    Quote Originally Posted by http://www.cplusplus.com/reference/clibrary/cstdio/gets.html
    First, the ending newline character is not included with gets while with fgets it is. And second, gets does not let you specify a limit on how many characters are to be read, so you must be careful with the size of the array pointed by str to avoid buffer overflows.
    Unless it's read but not included...?

  5. #5
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Unless it's read but not included...?
    You get a gold star. gets() removes the '\n' from the stream but doesn't store it in the array. The standard tells you that, but you can also test it with a quick program:
    Code:
    #include <stdio.h>
    
    
    int main( void ) {
      char s[BUFSIZ];
    
      gets( s );
      puts( "Done with gets()" );
      getchar();
      puts( "Done with getchar()" );
    
      return 0;
    }
    The getchar() call will block because there's nothing in the stream to read, not even a '\n'.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by daghenningsorbo View Post
    hey.
    when I use gcc to compile my .c-files it complains about the gets library function and says that "it is dangerous and should not be used". anyone know why?

    dagH
    gcc has nothing to do with it. It is the LINKER which complains. And you should heed the warning.

Popular pages Recent additions subscribe to a feed