On Aug 24, 2018, at 5:17 AM, ron(a)ronnatalie.com wrote:
For instance:
int x;
daddr_t* y;
u.b_un.b_words = &x;
y = u.b_un.b_daddr;
One would typically *not* do this.
This is syntactically correct, but the code will blow up bizarrely on
machines where the int* and long* pointers are not in the same format.
Now if the datatype was a void* and we did this:
u.b_vp = & x;
y = u.b_vp;
Provided there weren't any alignment issues, this would work as the
conversion to and from void* would result in the right pointer value.
For example, given
union {
caddr_t b_addr; /* low order core address */
int *b_words; /* words for clearing */
struct filsys *b_filsys; /* superblocks */
struct dinode *b_dino; /* ilist */
daddr_t *b_daddr; /* indirect block */
} b_un;
You'd use it something like this:
struct b_un x, y, z, *w;
struct filesys fs;
daddr_t d;
...
x.b_words = &b;
y.b_filsys = &fs;
z.b_addr = &d;
w = &x;
These come about because in different contexts different ptrs
may be used but one would not do any type conversion. Said another
way, if you replaced the union with a struct, type punning code
would fall apart but legit code would continue working (except
in the case where the *size* of the object is significant).