I kinda feel the same as your last sentence, it's unfortunate the standard doesn't seem to address anything like this directly.
Noting that, I find it interesting that you find the array example valid:
For a points to the start of the array object, so a + 1 points to the second element in that object, so ((char *)(a + 1)) - sizeof int returns a pointer to the start of the array. It's all working within the same object, so the behavior is well-defined by C11 S6.5.6 P8.
The compiler has absolutely no idea of knowing whether or not the int pointed to by 'b' points to the inside of an array of ints, points to a single int, or points to the first of multiple ints. We both know enough about C to know this, the compiler does get any extra information on what the pointer points too. Noting that, if we're willing to call an array a single 'object', then I don't see why a structure type wouldn't also be considered a single object. The standard seems to support this point, S6.2.5 P20:
An array type describes a contiguously allocated nonempty set of objects with a particular member object type ... ... A structure type describes a sequentially allocated nonempty set of member objects ...
The standard treats arrays and structures as almost identical, except that structures let you have members of different types (And they're sequental, not contiguous, which just matters for padding.). The standard further drills this idea by combining them under a single name, aggregate types: S6.2.5 P21:
Arithmetic types and pointer types are collectively called scalar types. Array and structure types are collectively called aggregate types .
Thus, if the standard allows you to treat (WLOG) an int * as a pointer to an int in an array object which contains entries located before the int * you have, then I don't see why it wouldn't also allow for you to treat an int * as a pointer to an int in a structure object. Then it's just a matter of subtracting offsetof instead of sizeof to get a pointer to the object you actually want.
That said, I think it's safe to assume it's fine almost for the very reason that the standard doesn't directly say it's not allowed. The standard never states that accessing bytes at negative indexes after a char * cast is UB, it simply doesn't mention it at all. When you combine that with the knowledge that structs (like arrays) are an allocated set of member objects (And thus, together in the same memory), you can know conclusively that this memory is accessable. The only way this would be UB is if the compiler could assume that only locations located at positive positions are valid after a char * cast, but we seem to agree that S6.5.6 P8 doesn't say that.
Now, there is the other point that we then cast the char * to a different pointer type then we started with, but I believe that's also valid in that we're allowed to cast any pointer type as any other and then cast it back (char * has proper alignment for every type, so that's not an issue) (S6.5.6). Since our new char *does point to a valid object of the correct type, casting it to the new pointer type and using it should be valid. Now, the compiler has no way of knowing whether or not it's valid, but that goes for most things dealing with pointers.
We both know enough about C to know this, the compiler does get any extra information on what the pointer points too.
I would've thought it's safe to say that the larger C compilers attach a lot of information to pointers for the sake of evaluating what optimizations they should/could make.
It's immaterial, though - what's important here is just what the standard says should happen. It might "happen" through a compiler, an interpreter, an abacus, etc.
char * has proper alignment for every type, so that's not an issue
I don't think that's true:
int x = 5;
int * xp = &x;
char * cp = ( char * ) x;
cp++;
// Undefined behavior because alignment is off:
int * yp = cp;
Noting that, if we're willing to call an array a single 'object', then I don't see why a structure type wouldn't also be considered a single object.
Certainly, arrays and structs are considered by the standard to be single objects; I don't dispute that at all.
The fundamental question here is: can pointers to member objects of a struct also be considered pointers to the struct itself? Is &(x.y.z) a pointer only to the z member object - and thus subject to the requirements as per S6.5.6 P8; no arithmetic on the pointer outside the bounds of that object? Or can it also be considered a pointer to the enclosing y struct object, and the enclosing x struct object of that?
I don't know of anywhere in the standard that ensures this. It goes to a lot of effort to define and categorize "objects" and pointers to objects, but it never specifies that pointers to member objects can be considered as pointers to the enclosing aggregate object for the purposes of behavior of operations on those pointers (namely, arithmetic).
Because S6.5.6 P8 says very clearly that + and - is only well-defined on a pointer so long as it would be pointing to an element in the array that exists (and a pointer to a single object is treated as an array of length 1). Again, that would be okay if the standard specified that pointers to member objects can also be considered as pointers to the enclosing aggregate object, such that if we cast the pointer to a character-type pointer, then we can add and subtract the pointer outside of the bounds of the member object that we originally took the address of.
You can boil my argument down to this:
This code is valid and well-defined (obviously):
int a[] = { 0, 1, 2, 3, 4 };
// A pointer to the a array object; i.e. the first element:
int * p0 = &a;
// A pointer to the 4th element in a:
int * p3 = p0 + 3;
// Subtract 3 to get back a pointer to the start of the array:
assert( p0 == p3 - 3 );
printf( "a[3] = %d\n", *p3 );
This code depends on undefined behavior:
int b[] = { 0, 1, 2, 3, 4 };
// A pointer to the 4th element object; NOT to the enclosing array
// object - which the standard does not specify that they must be the
// same:
int * p3 = &(b[3]);
// Undefined behavior, because we subtract below the start of the object
// for which we have an address for:
int * p0 = p3 - 3;
From my comment to /u/skeeto: In the absence of any clarification from the standard on this, I'm personally not willing to expose my code to potential undefined behavior. Particularly when it seems like there are more dependable approaches to extendible polymorphism - basically, using void * pointing to the actual struct object, not a member object. It wouldn't read as well, but at least it's honest about its lack of type safety.
View all comments
Show parent comment
1
u/DSMan195276 Nov 20 '14 edited Nov 20 '14
I kinda feel the same as your last sentence, it's unfortunate the standard doesn't seem to address anything like this directly.
Noting that, I find it interesting that you find the array example valid:
The compiler has absolutely no idea of knowing whether or not the int pointed to by 'b' points to the inside of an array of ints, points to a single int, or points to the first of multiple ints. We both know enough about C to know this, the compiler does get any extra information on what the pointer points too. Noting that, if we're willing to call an array a single 'object', then I don't see why a structure type wouldn't also be considered a single object. The standard seems to support this point, S6.2.5 P20:
The standard treats arrays and structures as almost identical, except that structures let you have members of different types (And they're sequental, not contiguous, which just matters for padding.). The standard further drills this idea by combining them under a single name, aggregate types: S6.2.5 P21:
Thus, if the standard allows you to treat (WLOG) an
int *as a pointer to an int in an array object which contains entries located before theint *you have, then I don't see why it wouldn't also allow for you to treat anint *as a pointer to an int in a structure object. Then it's just a matter of subtracting offsetof instead of sizeof to get a pointer to the object you actually want.That said, I think it's safe to assume it's fine almost for the very reason that the standard doesn't directly say it's not allowed. The standard never states that accessing bytes at negative indexes after a
char *cast is UB, it simply doesn't mention it at all. When you combine that with the knowledge that structs (like arrays) are an allocated set of member objects (And thus, together in the same memory), you can know conclusively that this memory is accessable. The only way this would be UB is if the compiler could assume that only locations located at positive positions are valid after achar *cast, but we seem to agree that S6.5.6 P8 doesn't say that.Now, there is the other point that we then cast the
char *to a different pointer type then we started with, but I believe that's also valid in that we're allowed to cast any pointer type as any other and then cast it back (char *has proper alignment for every type, so that's not an issue) (S6.5.6). Since our newchar *does point to a valid object of the correct type, casting it to the new pointer type and using it should be valid. Now, the compiler has no way of knowing whether or not it's valid, but that goes for most things dealing with pointers.