Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@cs.Helsinki.FI>
Subject: Re: gcc question
Date: Tue, 4 Jun 1996 10:46:23 +0300 (EET DST)	[thread overview]
Message-ID: <1119e18f49257ed2c95b4499278e0b94.axp-list@archive.invalid> (raw)
In-Reply-To: <199606032221.PAA15714@koala.azstarnet.com>

On Mon, 3 Jun 1996, David Mosberger-Tang wrote:
> 
> Please note that it is _not possible_ to initialize unions in ANSI C.

Actually, it's possible (umm - raincheck on the standard here, I'm too 
lazy to look it up, but I'm fairly certain). The standard allows you to 
initialize unions, but only the _first_ entry in a union.

> And there are good reasons why this is so: mgetty fails to compile
> under Linux/Alpha because there a pointer is 64 bits wide whereas an
> int is only 32 bits wide.  The patch below makes use of a GNU C
> extension.  With the patch, mgetty will compile as long as GCC is
> used.  However, it would be better to initialize the variants at
> runtime, since that would make the program more portable.

Another possibility is to put the largest entity first (the pointer), and 
then always initialize even the integers as pointers (rather than 
initializing the pointers as integers). That will then work on 
little-endian machines like the alpha (and will work on any big-endian 
machines where the sizes match, of course, taking care of most of them)...

A third possibility is to use "long" instead of "int" in the union, and casts
to (long) instead of (int) to initialize the entries. That's probably just
about as portable as this is going to get. I agree with David that the right
way to do this is to do the init at run-time, but that is often fraught with
other problems. Maybe an updated C standard will make the gcc extension a 
standard feature some day.

Using a gcc-specific feature may be the best thing to do in this case, 
but if somebody wants to use DEC cc without hacking the code to death, 
I'd just suggest using the "long" approach.

		Linus

       reply	other threads:[~1996-06-04  7:46 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <199606032221.PAA15714@koala.azstarnet.com>
1996-06-04  7:46 ` Linus Torvalds [this message]
1996-06-04 14:50 gcc question David Mosberger-Tang
     [not found] <Pine.LNX.3.91.960604103638.1696H-100000@linux.cs.Helsinki.FI>
1996-06-04 12:22 ` Michael K. Johnson
  -- strict thread matches above, loose matches on Subject: below --
1996-06-03 22:21 David Mosberger-Tang
1996-06-03 21:13 Brian Wood
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox