5

In c99, my understanding is that comparing two pointers which do not point within the same aggregate results in undefined behavior. Given an aggregate A, a pointer p_good which is known to point within A, and a pointer p_unknown which may or may not point within A, is it possible to construct a portable test with defined behavior which determines whether it is safe to compare p_good and p_unknown?

Obviously, this test cannot itself fall afoul of the restrictions on comparing pointers.

I suspect that the answer is 'no', but I'd be happy to be shown otherwise.

11
  • 1
    What do you mean by 'aggregate'? And what are you exactly trying to achieve? Aug 28, 2012 at 18:30
  • Do you have a pointer to A? Or just two pointers that point into A? Aug 28, 2012 at 18:33
  • 3
    @eq there is no UB in comparing pointers from distinct arrays for equality, but it is UB to compare pointers from distinct arrays for inequality, i.e. <, <=, >, >=. Aug 28, 2012 at 18:36
  • 2
    If you have an array and a candidate pointer, you can compare (==) this pointer to each possible element pointer, and see if it is equal to any of them. So, technically, the answer is yes :) Aug 28, 2012 at 18:47
  • 2
    This question is related: stackoverflow.com/questions/4023320/… Aug 28, 2012 at 19:55

1 Answer 1

5

You commented:

Another way to frame the question would be like this: Given the definition of an aggregate 'A' and a pointer p, is it possible to answer the question 'does p point within A' without violating the rule on inequality testing of pointers to different aggregates

The only way I can interpret this meaningfully is that you either have an object of type Aggregate type or a pointer to one. Then the answer is simple:

Pseudo-code:

bool p_in_A = false;
for (each element in Aggregate A)
    if (&element == p)
        p_in_A = true;

There is no way to tell whether a stray pointer belongs to an unknown aggregate object (or points to "between" elements in an aggregate).

4
  • Yes, this makes sense. I agree there is no way to do it in the case of an unknown aggregate, but in my case I do know enough about A to use the iterated equality check. Interesting that avoiding the UB requires a O(sizeof(A)) algorithm.
    – acm
    Aug 28, 2012 at 19:16
  • 1
    @acm, for an agreagate of unknown type where you only know the base pointer and the size you would have to do all this on a byte base by casting your pointers to unsigned char*. Aug 28, 2012 at 19:18
  • @JensGustedt In the motivating case, I do know A's type, and it happens to be char[], so it will work without casting.
    – acm
    Aug 28, 2012 at 19:19
  • @JensGustedt, yes. The most important part is a way of calculating the base address of the aggregate (in addition to it's size)
    – eq-
    Aug 28, 2012 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.