Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 11 Nov 2002 11:18:28 -0500 (EST)
From:      Thomas David Rivers <rivers@dignus.com>
To:        brandt@fokus.gmd.de, gallatin@cs.duke.edu
Cc:        FreeBSD-current@FreeBSD.ORG
Subject:   Re: gcc 3.2.1 optimization bug ?
Message-ID:  <200211111618.gABGIS155735@lakes.dignus.com>
In-Reply-To: <20021111163918.Y13339-100000@beagle.fokus.gmd.de>

next in thread | previous in thread | raw e-mail | index | archive | help
Harti Brandt <brandt@fokus.gmd.de> wrote:
> 
> Well, I just had a long discussion with a collegue about the topic. The
> main problem is in the ISO-C standard, section 6.7 point 4 which states:
> 
> All declarations in the same scope that refer to the same object or
> function shall specify compatible types.
> 
> This makes any fiddeling with a pointer of a type, that is different from
> the type of the variable illegal. It also makes fiddeling with unions and
> structs illegal. As far as I understand types are compatible when they
> have the same type and type qualifiers. Structs are partly compatible when
> they start with fields of the same type. So the only way to legally swap
> integer values of any size is via arithmetic. Of course unions and
> pointers may work with the given compiler version and optimisation level.
> But, nothing in the standard guarantees you anything.

 That's it exactly!

> 
> It may be possible that 6.5 (7) the last sentence allows you to access your
> 32-bit or 64-bit value character-wise (although I'm not sure).
> 
> harti
> 

The C99 standard does seem to add an "exception" for access via
character pointers.  The C89 standard did not have that clause.

As far as the union question - the answer is that you cannot
"read" from a union from a different member than it was "stored"
to.

For example:

   char ch;
   union {
      int x;
      char c;
   } my_union;

  my_union.x = 10;
  ch = my_union.c;

Technically, this is illegal in ANSI C, and not guaranteed to "work".  Although 
it will likely "work" in just about every reasonable environment.
(For some definition of "work" :-) )

	- Dave Rivers -

--
rivers@dignus.com                        Work: (919) 676-0847
Get your mainframe programming tools at http://www.dignus.com


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200211111618.gABGIS155735>