Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 11 Nov 2002 17:33:16 +0100 (CET)
From:      Harti Brandt <brandt@fokus.gmd.de>
To:        Thomas David Rivers <rivers@dignus.com>
Cc:        brandt@fokus.gmd.de, <gallatin@cs.duke.edu>, <FreeBSD-current@FreeBSD.ORG>
Subject:   Re: gcc 3.2.1 optimization bug ?
Message-ID:  <20021111172959.W32091-100000@beagle.fokus.gmd.de>
In-Reply-To: <200211111618.gABGIS155735@lakes.dignus.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, 11 Nov 2002, Thomas David Rivers wrote:

TDR>Harti Brandt <brandt@fokus.gmd.de> wrote:
TDR>>
TDR>> It may be possible that 6.5 (7) the last sentence allows you to access your
TDR>> 32-bit or 64-bit value character-wise (although I'm not sure).
TDR>
TDR>The C99 standard does seem to add an "exception" for access via
TDR>character pointers.  The C89 standard did not have that clause.

Yes, thats the 6.5 (7) clause, but I'm not sure, because it does not speak
about pointers. I don't fully understand what they mean :-(

TDR>As far as the union question - the answer is that you cannot
TDR>"read" from a union from a different member than it was "stored"
TDR>to.
TDR>
TDR>For example:
TDR>
TDR>   char ch;
TDR>   union {
TDR>      int x;
TDR>      char c;
TDR>   } my_union;
TDR>
TDR>  my_union.x = 10;
TDR>  ch = my_union.c;
TDR>
TDR>Technically, this is illegal in ANSI C, and not guaranteed to "work".  Although
TDR>it will likely "work" in just about every reasonable environment.
TDR>(For some definition of "work" :-) )

Hmm, I though the following would work:

void
foo(unsigned short *s)
{
	unsigned short temp;

	temp = s[0];
	s[0] = s[1];
	s[1] = temp;
}

main()
{
	int i = 0x12345678;

	foo(&i);
	printf("%08x\n", i);
}

because how would the compiler in main() know, that you do something wrong
in foo(). But... if you compile this with -O5, it does not work! This is
because the compiler inlines foo() into main and the program prints junk like
0x12342804.

harti
-- 
harti brandt, http://www.fokus.gmd.de/research/cc/cats/employees/hartmut.brandt/private
              brandt@fokus.gmd.de, brandt@fokus.fhg.de


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?20021111172959.W32091-100000>