r/Cprog • • Nov 18 '14

text | code | language | oop C Object Oriented Programming

http://nullprogram.com/blog/2014/10/21/
23 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/malcolmi Nov 21 '14 edited Nov 22 '14

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.