The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
From: <ron@ronnatalie.com>
To: "'Bakul Shah'" <bakul@bitblocks.com>
Cc: 'TUHS' <tuhs@minnie.tuhs.org>
Subject: Re: [TUHS] C++ / Kernel
Date: Fri, 24 Aug 2018 08:17:52 -0400	[thread overview]
Message-ID: <1d5901d43ba4$7bc413b0$734c3b10$@ronnatalie.com> (raw)
In-Reply-To: <4130AFB4-1740-46BC-AA98-F9D01549049C@bitblocks.com>

>> 	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).



  parent reply	other threads:[~2018-08-24 12:18 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-23 14:42 ron
2018-08-23 17:24 ` Clem Cole
2018-08-23 20:21 ` Bakul Shah
2018-08-23 22:17   ` ron
2018-08-23 22:28     ` Nevin Liber
2018-08-23 22:48       ` Clem Cole
2018-08-23 23:14     ` Steffen Nurpmeso
2018-08-24 14:13       ` Steffen Nurpmeso
2018-08-24 14:32         ` ron
2018-08-24 18:15           ` Steffen Nurpmeso
2018-08-26 16:34             ` Paul Winalski
2018-08-27 16:31               ` Steffen Nurpmeso
2018-08-24  1:41     ` Bakul Shah
2018-08-24 10:41       ` Pete Turnbull
2018-08-24 12:17       ` ron [this message]
2018-08-24 18:36         ` Bakul Shah
2018-08-24 18:38           ` ron
2018-08-24  1:58     ` Dan Cross
2018-08-24  3:04       ` Clem cole
2018-08-24 14:01         ` Dan Cross
2018-08-24 13:22 ` Derek Fawcus
2018-08-24 16:59   ` Steffen Nurpmeso
2018-08-23 23:29 Noel Chiappa
2018-08-23 23:42 ` ron
2018-08-24  0:30   ` Clem Cole
2018-08-24  2:05     ` Bakul Shah
2018-08-24 12:21     ` ron
2018-08-24  1:27 Noel Chiappa
2018-08-24  2:52 ` Clem cole
2018-08-24  7:30 Paul Ruizendaal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='1d5901d43ba4$7bc413b0$734c3b10$@ronnatalie.com' \
    --to=ron@ronnatalie.com \
    --cc=bakul@bitblocks.com \
    --cc=tuhs@minnie.tuhs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).