This is the mail archive of the gcc-patches@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]
Other format: [Raw text]

Re: [PATCH] fix aliasing for chars in global structure


On Mon, Mar 11, 2002 at 10:01:38AM +0000, Joseph S. Myers wrote:
> On Mon, 11 Mar 2002, Zack Weinberg wrote:
> 
> > extern struct first F;
> 
> > I am not sure what the literal text of the standard says, but my
> > impression of the intent is that both of these are undefined.
> 
> They might or might not be defined in this case of extern struct first F -
> see Nick Maclaren's "What is an object?" document (WG14 reflector sequence
> 9350 or ask him for a copy).  If the pointers are to within allocated

Is the reflector archived on the public Web, and if so, where?

> memory, which in the original example they may be, I see nothing that
> would stop there from being aliasing.

The standard is known to be defective (if only through inclarity) in
this area; we should be trying to figure out what it _should_ specify.

First, I'd argue that objects which arise from malloc should be
treated equivalently to objects defined at translation time, and
therefore it should not matter whether we have extern struct first F
or pointers into allocated memory.  The compiler does know more about
a global variable, but I think the type aliasing rules give it enough
leeway to assume that two pointers, one to struct first, one to struct
second, point to distinct and nonoverlapping objects.  (Hmm, in

struct first; struct second;

int compare(struct first *a, struct second *b)
{
  return a == b;
}

could we optimize down to "return 0;" ?)

Second, I think the intent of the standard was a lot closer to what
you describe as a misconception, i.e. that it was access through a
true (signed/unsigned) char * that was allowed to alias anything.
Rationale, page 47:

	In principle, then, aliasing only need be allowed for when the
	lvalues all have the same type.  In practice, the C89
	Committee recognized certain prevalent exceptions:
[...]
	* Character pointer types are often used in the bytewise
	  manipulation of objects; a byte stored through such a
	  character pointer may well end up in an object of any type.

>From the programmer's point of view, "only char * can alias anything"
makes a lot more sense than "a character element of an aggregate,
accessed through a pointer to the aggregate type, can too."

This is starting to look like DR material.

zw


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