[TUHS] C++ / Kernel

ron at ronnatalie.com ron at ronnatalie.com
Fri Aug 24 22:17:52 AEST 2018


>> 	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;

>  Note that this is a legitimate use of union. That is, unless I
misunderstood what you meant by it, there is no "conversion by union" as you
call it or "cheating"
> as I call it or type punning. There is no put one thing in and take
another thing out. Now it may be that someone misused such a union. This is
easy to do as, unlike Pascal, C has no tagged variant record & it is user's
responsibility to use it right.

It's not legitimate.   It's undefined behavior in the standards and as
someone has already posted, Ritchie warned about the issue at the time.
It is undefined (currently) or in the past (machine dependent) behavior  to
store in one element of a union and retrieve it by another.

For instance:
       int x;
       daddr_t* y;

      u.b_un.b_words = &x;
      y = u.b_un.b_daddr;

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.

I can think of three different machines I've worked on that this is a
problem.   The Delelcor HEP was the one I got caught on because I was
porting 4.2BSD to it.
The Univac (er, um, Sperry) 1100 series and I think the DEC10 by derivation
have the partial word (i.e., anything less than a full 36 bits) encoded in
the pointers.
The Crays (being word addressed machines, character types are "simulated")
have distinct word and char pointers.

void* has to be format the same as char* (don't get me started on this
idiocy, while it worked for the PDP-11, it's an inane assumption that the
basic character type is the same as
the smallest addressable storage unit.    THIS is one of the things that
should have been fixed about the time void* was conceived).





More information about the TUHS mailing list