[comp.lang.c] Portable use of unions

davidsen@sixhub.UUCP (Wm E. Davidsen Jr) (12/05/89)

  I recently got a complaint that one of my programs was non-portable
and wouldn't run under a certain compiler. I isolated a tiny fragment
which causes the error message, and would like to know if anyone can
explain why this doesn't work other than the compiler being broken.

================ start code

  /* test of code generated for unions */
  
  char buf[30];		/* simple char buffer		*/
  
  union {
    int t_sscr;		/* subscript of location	*/
    char *t_adrs;		/* actual location		*/
  } demo;
  
  main() {
    demo.t_sscr = 4;
    demo.t_adrs = &buf[demo.t_sscr];
  }

================ end code

  The next to last line (2nd assignment) is flagged with a message of
"conflicting use of union fields in a single statement." I tried it on
seven common compilers and they all liked it. The int value of the union
holds the subscript of the start of the string, while the pointer value
will hold the actual starting address. Obviously only one can be valid
at any given time.

  I'm willing to be shown that there's a problem here, but only if
either K&R or ANSI says there is. I don't care if people have broken
compilers, that's their problem, right?
-- 
	bill davidsen - sysop *IX BBS and Public Access UNIX
davidsen@sixhub.uucp		...!uunet!crdgw1!sixhub!davidsen

"Getting old is bad, but it beats the hell out of the alternative" -anon

ajr@inesc.UUCP (Julio Raposo) (12/05/89)

In article <203@sixhub.UUCP>, davidsen@sixhub.UUCP (Wm E. Davidsen Jr) writes:
> 
> ================ start code
> 
>   /* test of code generated for unions */
>   
>   char buf[30];		/* simple char buffer		*/
>   
>   union {
>     int t_sscr;		/* subscript of location	*/
>     char *t_adrs;		/* actual location		*/
>   } demo;
>   
>   main() {
>     demo.t_sscr = 4;
>     demo.t_adrs = &buf[demo.t_sscr];
>   }
> 
> ================ end code
> 

	This code is non-portable because in the C language the evaluation
	order is undefined. The compiler may evaluate first demo.t_adrs and
	when it tries to evaluate demo.t_sscr complains that the address is
	already beeing used.

	For that example I would write: (coments erased)

 
 ================ start code
 
   char buf[30];
   
   union {
     int t_sscr;
     char *t_adrs;
   } demo;
   
   main() {
     char *aux;
     demo.t_sscr = 4;
     aux = &buf[demo.t_sscr];
     demo.t_adrs = aux;
   }
 
 ================ end code
 

			Antonio Julio Raposo (ajr@inesc, LISBOA, PORTUGAL)

c9h@psuecl.bitnet (12/06/89)

In article <203@sixhub.UUCP>, davidsen@sixhub.UUCP (Wm E. Davidsen Jr) writes:
>
>   The next to last line (2nd assignment) is flagged with a message of
> "conflicting use of union fields in a single statement." I tried it on
> seven common compilers and they all liked it. The int value of the union
> holds the subscript of the start of the string, while the pointer value
> will hold the actual starting address. Obviously only one can be valid
> at any given time.

This appears to be a poor attempt by the compiler to insure that the [assumed
stupid] programmer doesn't use both at the same time.  Unfortunately, this
only causes problems when you actually have a valid use for it.

>   I'm willing to be shown that there's a problem here, but only if
> either K&R or ANSI says there is. I don't care if people have broken
> compilers, that's their problem, right?

Yeah.  <*heavy* sigh>  I concur.  It's their problem.

--
- Charles Martin Hannum II       "Klein bottle for sale ... inquire within."
    (That's Charles to you!)     "To life immortal!"
  c9h@psuecl.{bitnet,psu.edu}    "No noozzzz izzz netzzzsnoozzzzz..."
  cmh117@psuvm.{bitnet,psu.edu}  "Mem'ry, all alone in the moonlight ..."

bill@twwells.com (T. William Wells) (12/08/89)

In article <203@sixhub.UUCP> davidsen@sixhub.UUCP (bill davidsen) writes:
:     demo.t_adrs = &buf[demo.t_sscr];
:
:   The next to last line (2nd assignment) is flagged with a message of
: "conflicting use of union fields in a single statement."

Incorrectly. The compiler is broken.

---
Bill                    { uunet | novavax | ankh | sunvice } !twwells!bill
bill@twwells.com