This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: aliasing warnings [patch]
- To: zack AT bitmover dot com
- Subject: Re: aliasing warnings [patch]
- From: Mark Mitchell <mark AT codesourcery dot com>
- Date: Thu, 16 Sep 1999 13:07:52 -0700
- Cc: jason AT cygnus dot com, egcs AT egcs.cygnus dot com, egcs-patches AT egcs.cygnus dot com
- Organization: CodeSourcery, LLC
- References: <u9wvtqemw1.fsf@yorick.cygnus.com><jason@cygnus.com><199909161938.MAA15782@zack.bitmover.com>
Consider this fragment:
{
char buf[sizeof(struct foo)];
struct foo *x = (struct foo *)buf;
x->bar = value1;
x->baz = value2;
...
fwrite(buf, sizeof(struct foo), 1, file);
}
I believe this is well-defined.
Not really. For one thing, you don't know buf is suitably aligned.
But, more appropriately to this discussion, the aliasing rules don't
let you do this. The underlying type of the fields is `char' and
you're putting `int' into them. That's no different than:
char c[4];
*((int *) &c[0]) = 3;
say.
It's all OK if you say `char *buf = (char*) malloc (sizeof (struct
foo));' though. Calling `malloc' doesn't impose any type on the
storage.
--
Mark Mitchell mark@codesourcery.com
CodeSourcery, LLC http://www.codesourcery.com