[net.bugs] Array of unions

rmc@lanl.ARPA (06/27/85)

	I defined an array of unions as follows:

	    typedef union {
		unsigned char *bytes;
		unsigned long *words;
	    } block;

	    block sched[NKEYS];

	Now if I try to pass sched[i] as a parameter,
the C compiler bombs with "Fatal error in /lib/ccom".
Maybe I should have known that arrays of unions don't
compile, but couldn't the compiler leave me a more
descriptive message?  I'll I'm asking for is a line
number -- that bug took me a couple of hours to locate.

			R. Martin Chavez
			(rmc@lanl.ARPA, chavez@harvard.ARPA)

gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (06/28/85)

There's nothing wrong with arrays of unions.
Perhaps either your compiler or your code is broken.
You did not give enough information for anyone to help.

rmc@lanl.ARPA (06/28/85)

> 
> 	I defined an array of unions as follows:
> 
> 	    typedef union {
> 		unsigned char *bytes;
> 		unsigned long *words;
> 	    } block;
> 
> 	    block sched[NKEYS];
> 
> 	Now if I try to pass sched[i] as a parameter,
> the C compiler bombs with "Fatal error in /lib/ccom".

	Here's an example of a program that didn't
compile under 4.2/4.3bsd (VAX).  The program DID
compile on a SUN, or with the portable C compiler
under SUMacC.

#define NKEYS 20
	    typedef union {
		unsigned char *bytes;
		unsigned long *words;
	    } block;


	main()
	{
	block sched[NKEYS];
	int i;
	i= 4;
	bar(i, sched);
	}

	bar (i, sched) block sched[];
	{
	foo(sched[i]);
	}

	foo (junk) block junk;
	{
	printf("%d\n",junk.bytes);
	}

			-- R. Martin Chavez
			(chavez@lanl.ARPA)

guy@sun.uucp (Guy Harris) (06/29/85)

> ...the C compiler bombs with "Fatal error in /lib/ccom".

That's the best it could give you; that message is the "cc" command's
cryptic way of telling you that the compiler pass whose binary is in
/lib/ccom got a signal and dumped core/MOS.  "cc" should dump the signal
name (NOT number) and an indication of whether core was dumped or not.  At
least that would give the poor user a bit better indication of what went
wrong, and might help a little bit in debugging the compiler.

	Guy Harris

jsdy@hadron.UUCP (Joseph S. D. Yao) (07/03/85)

In article <27694@lanl.ARPA> rmc@lanl.ARPA writes:
>> 	I defined an array of unions as follows:
>> 	    typedef union {
>> 		unsigned char *bytes;
>> 		unsigned long *words;
>> 	    } block;
>> 	    block sched[NKEYS];
>> 	Now if I try to pass sched[i] as a parameter,
>> the C compiler bombs with "Fatal error in /lib/ccom".
>	Here's an example of a program that didn't
>compile under 4.2/4.3bsd (VAX).  The program DID
>compile on a SUN, or with the portable C compiler
>under SUMacC.
>#define NKEYS 20
>	typedef union {
>		unsigned char *bytes;	unsigned long *words;
>	} block;
>	main()
>	{
>	block sched[NKEYS]; int i;
>	i= 4; bar(i, sched);
>	}
>
>	bar (i, sched) block sched[];
>	{ foo(sched[i]); }
>
>	foo (junk) block junk;
>	{ printf("%d\n",junk.bytes); }

As others have said, this should have worked.  Potential problems/
perturbations to test:
(1) in bar(), try changing sched[] to *sched.  As discussed ad
    infinauseum elsewhere, this  s h o u l d  make no difference.
(2) in bar(), pass &sched[i]; and in foo(), declare block *junk;.
    Also, of course, junk->bytes.  The construct you use there is
    relatively recent, and might cause problems that haven't been
    encountered before.  I know that passing structs as parameters
    is now old hat, but I think some compilers handle unions a
    little strangely.  Addresses (as in the old days) might be
    a bit better.
Good luck.  Write if it's found to work.
-- 

	Joe Yao		hadron!jsdy@seismo.{ARPA,UUCP}