Reputation:
c99's 7.20.3 states:
The order and contiguity of storage allocated by successive calls to the calloc, malloc ,and realloc functions is unspecified.
Why would this even be mentioned?
Comparing pointers pointing to different objects results in an undefined behavior. So does an attempt to read/write outside of the size of the allocated object.
As such, what's up with the concept of an order in allocated storage? Same goes for contiguity.
Just in case I would like to note that implementation specific "blah blah malloc blah heap blah blah manages memory" talks are offtopic here.
EDIT: for interested parties for some reason I originally wrote 'automatic' instead 'allocated' which is now fixed
Upvotes: 2
Views: 99
Reputation: 263217
It's a slight overspecification. It's harmless, intended to improve clarity, not to impose a specific requirement. It makes it clear that successive calls to malloc
do not necessarily result in contiguous allocation in memory.
If that sentence were deleted, the meaning of the standard as a whole would probably be unchanged.
Note that two distinct objects can be adjacent, and the standard explicitly acknowledges this. N1570 6.5.9p6 says that two pointers compare equal if "one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space". The sentence you quoted in your question is not necessary, since the order and contiguity would still be unspecified if the standard simply didn't specify them, but again, it doesn't hurt to be slightly more explicit than necessary.
Some clarifications on your question:
As user3386109 points out in a comment, the cited passage is not about automatic storage. The question was edited to correct this.
Comparing pointers to different objects for equality or inequality has well-defined behavior. It's only the relational operators (<
, <=
, >
, >=
) whose behavior is undefined. Pointers to distinct objects are, of course, always unequal.
If you think that discussions of implementation-specific features are off topic here, you have misunderstood. As it happens, no such considerations are necessary to answer your question.
Upvotes: 3