Thread: scanning and printing a string

  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 04:27 AM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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