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));
Printable View
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));
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.Quote:
You can realloc() a NULL pointer, if that's what you're asking.
What problem did that complainer identify as the cause for the crash?
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.
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.Quote:
Originally Posted by C99 Section 7.20.3.4 Paragraph 3
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.