realloc() and NULL mystery

This is a discussion on realloc() and NULL mystery within the C Programming forums, part of the General Programming Boards category; Code: int points_count = 1; int *new_x = NULL; new_x = realloc(new_x, points_count * sizeof (int)); Should it be a ...

  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,185
    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
    21,744
    What problem did that complainer identify as the cause for the crash?
    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

  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
    21,744
    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.
    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

  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, 04: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, 04:19 PM
  5. segfault on realloc
    By ziel in forum C Programming
    Replies: 5
    Last Post: 03-16-2003, 03:40 PM

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