This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: aliasing warnings [patch]



  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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]