Suppose the Standard had specified that an implementation must predefine a constant to indicate how it handles the realloc(ptr,0)
case, which is what "implementation defined" should really imply in scenarios where the number of possible behaviors is limited. In what scenarios would such a constant actually be useful?
Any portable and correct code would need to accommodate two possible reasons why realloc(ptr,0)
could return null:
The implementation is for some reason unable to satisfy the request, and returns null without disturbing the original storage, implying that calling code must call free(ptr);
.
The implementation is designed to interpret realloc(ptr,0)
as equivalent to free(ptr); return 0;
, implying that calling code must not call free(ptr);
.
While it would be theoretically possible for a program to test a predefined constant to determine how it should handle the null-return case, the amount of effort required to write portable code which would sometimes call realloc(ptr,0)
would generally exceed the amount required to write portable code that would never call that function with a size
value of 0.
Note that characterizing the action as Undefined Behavior means nothing more nor less than that the Standard imposes no requirements; it does not imply any judgment as to whether or not some treatments might be more or less useful than others in some circumstances.