Re: Why is C Standard Code Example Invalid?
- From: lawrence.jones@xxxxxxxxxxx
- Date: Tue, 15 Jan 2008 13:13:03 -0500
James Kuyper <jameskuyper@xxxxxxxxxxx> wrote:
The standard is quite clear on this: 6.5.2.3p5 says "... it is permitted
to inspect the common initial part of any of them anywhere that a
declaration of the complete type of the union is visible. ...". Since
the function body of f() is not such a location, it is not permitted.
The question of why it imposes such a requirement is far less clear to
me. There's probably a good reason, but quite frankly, I can't imagine
any way to implement structures and unions where this would cause a
problems.
Let's look at the significant part of the code in question:
int f(struct t1 * p1, struct t2 * p2)
{
if (p1->m < 0)
p2->m = -p2->m;
return p1->m;
}
Since p1 and p2 are pointing to incompatible structs and there's no
visible union that contains them both, the compiler is at liberty to
assume that changes to the struct pointed to by p2 have no affect on the
struct pointed to by p1. Thus, a reasonable code generation strategy
would be to load the value of p1->m into a register as part of the "if"
test, and then use the value in the register for the "return" rather
than re-fetching it from memory. When the structs pointed to by p1 and
p2 are, in fact, members of a (non-visible) union, that produces the
wrong answer. The C Standard says this a bug in the program rather than
a bug in the compiler.
-Larry Jones
What this games needs are negotiated settlements. -- Calvin
.
- References:
- Why is C Standard Code Example Invalid?
- From: Martin
- Re: Why is C Standard Code Example Invalid?
- From: James Kuyper
- Why is C Standard Code Example Invalid?
- Prev by Date: Re: Why is C Standard Code Example Invalid?
- Next by Date: Re: Why is C Standard Code Example Invalid?
- Previous by thread: Re: Why is C Standard Code Example Invalid?
- Next by thread: Re: Why is C Standard Code Example Invalid?
- Index(es):
Relevant Pages
|