L
Lauri Alanko
The standard (6.3.2.3#7) makes conversion between object types
well-defined when a converted pointer is "correctly aligned" for the
referenced type. But in what situations can a portable program trust
that a pointer of type A* is correctly aligned for conversion to B*?
Certainly when B is a character type (or void), or when the A* pointer
has been converted from the return value of a malloc call. But are
there any other cases?
How about the case when B is an incomplete type? Is the following
guaranteed to work?
typedef struct A { int x; } A;
typedef struct B B;
A a = { 42 };
B* bp = (B*) &a;
A* ap = (A*) bp;
ap->x = 54;
I can't see how this could go wrong, but could it be that B (which is
never ever completed) could somehow have stricter alignment
requirements than A, making the conversion undefined?
Thanks,
Lauri
well-defined when a converted pointer is "correctly aligned" for the
referenced type. But in what situations can a portable program trust
that a pointer of type A* is correctly aligned for conversion to B*?
Certainly when B is a character type (or void), or when the A* pointer
has been converted from the return value of a malloc call. But are
there any other cases?
How about the case when B is an incomplete type? Is the following
guaranteed to work?
typedef struct A { int x; } A;
typedef struct B B;
A a = { 42 };
B* bp = (B*) &a;
A* ap = (A*) bp;
ap->x = 54;
I can't see how this could go wrong, but could it be that B (which is
never ever completed) could somehow have stricter alignment
requirements than A, making the conversion undefined?
Thanks,
Lauri