steve@umigw.MIAMI.EDU (steve emmerson) (11/09/88)
(My apologies if this has been covered before---as I'm sure it has :-) I have a question on the validity and desireability of pointer comparisons in the following situation: I have two arrays. The elements of one array are structures and the elements of the other need to be references to those structures. These references could be either pointers or indicies. I wish to perform a consistancy check on the references; in particular, I wish to insure that they are valid (i.e. that the referred-to elements in do, in fact, exist). If indicies are used, I can simply perform range checking. If pointers are used, however, can I verify the references? My fear is that I cannot because the relational comparison of pointers is guaranteed to be valid only for pointers into the same array, and, in this particular situation, that is exactly what I'm trying to determine. Put succinctly, is the following expression true: TestPtr < StartPtr || TestPtr > StopPtr if, and only if, "TestPtr" points someplace outside the "StartPtr" to "StopPtr" range. Must I use indicies and accept their potentially slower performance? Thank you for your advice. -- Steve Emmerson Inet: steve@umigw.miami.edu [128.116.10.1] SPAN: miami::emmerson (host 3074::) emmerson%miami.span@star.stanford.edu UUCP: ...!ncar!umigw!steve emmerson%miami.span@vlsi.jpl.nasa.gov "Computers are like God in the Old Testament: lots of rules and no mercy"
steve@umigw.MIAMI.EDU (steve emmerson) (11/14/88)
Sorry to bring this up again, but I didn't get no satisfaction last time (i.e. no one responded to the main point in question). The question is this: is the following expression guaranteed to return true if and only if "TestPtr" points within the array: static struct foo array[NUM]; static struct foo BegPtr = array[0]; static struct foo BegPtr = array[NUM-1]; extern struct foo *TestPtr; /* could also come from a function call */ TestPtr >= BegPtr && TestPtr <= EndPtr; /* guaranteed to do what I want? */ Or is it "implementation defined" or "undefined". Note the "if and only if" condition, which means that if the expression is false, then "TestPtr" *must* point "outside" the array. I know about pointer comparisons only being guaranteed for pointers into the same array. What I don't know is what that means for the above expression. P.S. This is a contrived problem. I know the alternatives. -- Steve Emmerson Inet: steve@umigw.miami.edu [128.116.10.1] SPAN: miami::emmerson (host 3074::) emmerson%miami.span@star.stanford.edu UUCP: ...!ncar!umigw!steve emmerson%miami.span@vlsi.jpl.nasa.gov "Computers are like God in the Old Testament: lots of rules and no mercy"
tim@crackle.amd.com (Tim Olson) (11/15/88)
In article <185@umigw.MIAMI.EDU> steve@umigw.miami.edu (steve emmerson) writes: | The question is this: is the following expression guaranteed to return | true if and only if "TestPtr" points within the array: | | static struct foo array[NUM]; | static struct foo BegPtr = array[0]; | static struct foo BegPtr = array[NUM-1]; | extern struct foo *TestPtr; /* could also come from a | function call */ | | TestPtr >= BegPtr && TestPtr <= EndPtr; /* guaranteed to do what | I want? */ | | Or is it "implementation defined" or "undefined". Note the "if and only | if" condition, which means that if the expression is false, then "TestPtr" | *must* point "outside" the array. It is undefined. The "iff" condition also means that if the expression is true, then "TestPtr" must point *inside* the array, which is the problem here. Section 3.3.8 of the ANSI Draft says: "... If the objects pointed to are not members of the same aggregate or union object, the result is undefined." -- Tim Olson Advanced Micro Devices (tim@crackle.amd.com)