This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Undefined behavior due to 6.5.16.1p3
- From: Richard Biener <richard dot guenther at gmail dot com>
- To: Robbert Krebbers <mailinglists at robbertkrebbers dot nl>
- Cc: GCC Development <gcc at gcc dot gnu dot org>
- Date: Tue, 10 Mar 2015 09:51:43 +0100
- Subject: Re: Undefined behavior due to 6.5.16.1p3
- Authentication-results: sourceware.org; auth=none
- References: <54FDF3CE dot 6030507 at robbertkrebbers dot nl>
On Mon, Mar 9, 2015 at 8:26 PM, Robbert Krebbers
<mailinglists@robbertkrebbers.nl> wrote:
> I was wondering whether GCC uses 6.5.16.1p3 of the C11 standard as a license
> to perform certain optimizations. If so, could anyone provide me an example
> program.
>
> In particular, I am interested about the "then the overlap shall be exact"
> part of 6.5.16.1p3:
>
> If the value being stored in an object is read from another
> object that overlaps in any way the storage of the first
> object, then the overlap shall be exact and the two objects
> shall have qualified or unqualified versions of a compatible
> type; otherwise, the behavior is undefined.
struct X { int i; int j; };
int foo (struct X *p, struct X *q)
{
q->j = 1;
p->i = 0;
return q->j;
}
will optimize to return 1. If *p and *q were allowed to overlap
(&p->i == &q->j)
this would invoke undefined behavior.
Richard.