|
|
|
|
|
by 0x09
2108 days ago
|
|
The article doesn't touch on this, but in C pointer types are also the only kind that can be invalidated without any apparent change to their value: void *x = malloc(...);
free(x);
if(x); // undefined behavior
Note that this isn't about dereferencing x after free, which is understandably not valid. Rather the standard specifies that any use of the pointer's value itself is undefined after being used as an argument to free, even though syntactically free could not have altered that.This special behavior is also specifically applied to FILE* pointers after fclose() has been called on them. If there is some historical reason / architecture that could explain this part of the specification I would be interested to hear the rationale, this has been present in mostly the same wording since C89. |
|
[1] I think, but I'm not sure which versions of C/C++ added this guarantee