scanning and printing a string

This is a discussion on scanning and printing a string within the C Programming forums, part of the General Programming Boards category; I use DevC++ as my compiler. I used the following two codes: Code: #include <stdio.h> #include <string.h> #include <stdlib.h> int ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    11

    scanning and printing a string

    I use DevC++ as my compiler.

    I used the following two codes:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main()
    {
        char *p,c;
        gets(p);
        printf("%s", p);
        getch();
    }
    and the second code as
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main()
    {
        char *p,c;
        p = (char *)malloc(0);
        gets(p);
        printf("%s", p);
        getch();
    }
    the first code runs into a segmentation fault while the second one runs perfectly fine. What is the reason ? isnt doing malloc(0) same as not using the malloc() statement ?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    In first code snippet, you are using(writing) to un-initialized pointer. un-initialized does not mean it's pointing to NULL!
    for 2nd case, malloc(0) will return NULL. and of course gets() will deference that pointer resulting seg fault.
    Edit: I might be falling asleep. 2nd code runs fine? anyway you shouldn't be relying on that anyway. Try to check value of p after malloc(0).
    2nd Edit: laserlight is more correct regarding malloc(0).
    Last edited by Bayint Naung; 12-16-2010 at 03:27 AM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    This is what the 1999 edition of the C standard has to say about malloc(0):
    Quote Originally Posted by C99 Clause 7.20.3
    If the size of the space requested is zero, the behavior is implementation defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.
    Both your code samples exhibit undefined behaviour.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    thanks a lot (and for that quick reply)

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    So the second code works (due to compiler ?) but it may not work for all compilers. Am I correct ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    Quote Originally Posted by Prestige
    So the second code works (due to compiler ?) but it may not work for all compilers. Am I correct ?
    In a way, yes. However, it is not even guaranteed to work on whatever compiler you used (undefined behaviour, not just implementation defined behaviour, comes into play).

    Oh, and stop the use of the gets function as it is inherently vulnerable to buffer overflow (though that is a moot point in this case where you either don't have a buffer, or you have a zero size buffer).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help scanning in numbers and printing format
    By ArcadeEdge in forum C Programming
    Replies: 2
    Last Post: 10-12-2010, 05:13 PM
  2. Question on Classic big integer program
    By DaNxTh3xMaNx in forum C Programming
    Replies: 16
    Last Post: 09-14-2010, 10:11 PM
  3. Printing via COM port
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-06-2002, 07:30 AM

Tags for this Thread


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