Should it be a valid code or not?Code:int points_count = 1; int *new_x = NULL; new_x = realloc(new_x, points_count * sizeof (int));
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 ...
Should it be a valid code or not?Code:int points_count = 1; int *new_x = NULL; new_x = realloc(new_x, points_count * sizeof (int));
The only good is knowledge and the only evil is ignorance.
~Socrates
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.You can realloc() a NULL pointer, if that's what you're asking.
The only good is knowledge and the only evil is ignorance.
~Socrates
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
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
Oh, in that case he is mistaken, and you can quote him the standard:
Of course it is simpler to just use malloc() in this specific example.Originally Posted by C99 Section 7.20.3.4 Paragraph 3
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
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