3 ms·
They're from different objects, so it's not meaningful (according to the standard) to compare them. That's what makes it UB.
by Rusky 8y ago
They're from different objects, so it's not meaningful (according to the standard) to compare them. That's what makes it UB.
- jjnoakes 8y ago"Not meaningful" and "undefined behavior" have very different meanings. I agree it's not meaningful, but the standard does define the result of comparing pointers to two unrelated objects with "==", so it's not UB.
- Rusky 8y agoWhere? The post cites C11 section 6.5.8 paragraph 5, which seems to disagree with you. Of course == comparing unrelated pointers is something you need to be able to do, but relational comparisons (including those based on pointer arithmetic) don't seem to be defined anywhere...
- jjnoakes 8y agoRight, comparisons with < and > are UB, but I'm talking about ==, which is well-defined. It's hard to tell which operator "tzahola" was originally talking about, but I took "comparing the address" in this context (especially given his example code) to mean "==", or at least, include it, which makes the statement "comparing the addresses of stack variables is undefined behavior" incorrect in this context.
- tzahola 8y agoYou're right. There's no undefined behaviour for ==: Two pointers compare equal if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function, both are pointers to one past the last element of the same array object, or 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.