10
$\begingroup$

This is the first breaking change that C made, which was making realloc(ptr, 0) have UB instead of being roughly equivalent to free(ptr).

C has been known to be hesitant to making 'breaking' changes such as being extra careful to add _Ugly keywords that will not conflict with existing codebases.

So why did they see it necessary to undefine realloc(ptr, 0)? What problems did requiring that to have defined behavior cause?

$\endgroup$
3

1 Answer 1

3
$\begingroup$

Previously, realloc(ptr, 0) was implementation-defined. As far as portable programming is concerned, there's little operational difference between implementation-defined behavior and undefined behavior.

First of all, a portable program can't depend on a specific behavior. Not all implementations would treat realloc(ptr, 0) like free(ptr), so you could get memory leaks with some implementations.

Second, an implementation is permitted to provide a definition of the behavior of something that the standard says is undefined. So if you're writing a program that's targeted to a particular implementation, you can take advantage of its definition of this situation.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .