Thread: realloc() and NULL mystery

  1. #1
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88

    Question realloc() and NULL mystery

    Code:
    int points_count = 1;
    int *new_x = NULL;
    new_x = realloc(new_x, points_count * sizeof (int));
    Should it be a valid code or not?
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can realloc() a NULL pointer, if that's what you're asking.

  3. #3
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    You can realloc() a NULL pointer, if that's what you're asking.
    I know that. But I saw someone complaining about exactly this code that it would cause crash, although I haven't observed such a behavior myself.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What problem did that complainer identify as the cause for the crash?
    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

  5. #5
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    He just said that it would crash on this part of code. Then he was advised to rewrite this like

    new_x = realloc(NULL, points_count * sizeof (int));
    which is equivalent to
    new_x = malloc(points_count * sizeof (int));

    And now I just don't get it, since there would be an implicit cast to void * anyway.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, in that case he is mistaken, and you can quote him the standard:
    Quote Originally Posted by C99 Section 7.20.3.4 Paragraph 3
    If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size.
    Of course it is simpler to just use malloc() in this specific example.
    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

  7. #7
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    laserlight
    Okay, thanks then. For a couple of minutes I almost started to believe that there is a difference between passing bare NULL or a pointer initialized to it.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  4. A bullet-proof fgets wrapper?
    By n7yap in forum C Programming
    Replies: 15
    Last Post: 12-16-2004, 05:19 PM
  5. segfault on realloc
    By ziel in forum C Programming
    Replies: 5
    Last Post: 03-16-2003, 04:40 PM

Tags for this Thread