The C standard mandates that all pointers to union
s have the same representation and alignment requirements.
It mandates the same for all pointers to struct
s.
Thus my question:
Why does the standard not mandate that pointers to union
s have the same representation and alignment requirements as pointers to struct
s? (I would very much appreciate an example of an implementation taking advantage of this.)
Or did I simply miss the relevant text?
The relevant quote from the draft standard n1570 (C11 final draft):
6.2.5 Types § 28
A pointer to
void
shall have the same representation and alignment requirements as a pointer to a character type.48) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.
Neither the 1989 ANSI C Rationale nor the ISO C99 Rationale discusses this.
I doubt that there's any strong reason for the lack of such a requirement. Probably the committee didn't want to impose unnecessarily strict requirements. I also doubt that there are any real-world implementations where pointers to structs and pointers to unions don't have the same representations.
Consider, for example, that a struct can contain a single union member, and vice versa, so there's probably no good reason for an implementation to use different representations -- nor is there any good reason for the standard to require all implementations to use identical representations.
The reason all struct pointers "smell alike" is to permit the use of pointers to incomplete types. For example:
struct foo;
void func(struct foo *param);
struct foo { /* member declarations */ };
The compiler doesn't have to know anything about struct foo
other than that it's a struct
type to know how to generate a call to func()
.
The same applies to incomplete union types. But an incomplete struct type cannot be completed as a union, or vice versa, so there's no great benefit in being able to assume that they have the same representation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With