Re: Neatest way to get the end pointer?
- From: ymuntyan@xxxxxxxxx
- Date: Tue, 12 Feb 2008 11:36:33 -0800 (PST)
On Feb 12, 8:38 am, r...@xxxxxxxxxxxxxxxxxxxxxx (Richard Bos) wrote:
"Chris Thomasson" <cris...@xxxxxxxxxxx> wrote:
"Tomás Ó hÉilidhe" <t...@xxxxxxxxxxx> wrote in message
I was referring to the rule whereby you can't write to union member A
and then go on to read from union member B.
I have always wondered why that rule exists. For instance:
typedef union foo_u {
long int a;
int b;
} foo;
foo f = { 0 };
f.a = 1;
printf("%d\n", f.b);
Possibly because it is hard to write a rule which reliably forbids the
kind of union hacks which would break, but allows the ones that
wouldn't, while the kind of aliasing hackery which one can do with
unions is also done, and more reliably, with pointer aliasing.
It's not true. Some TC explicitly made type punning using
unions work (unless you step onto a trap representation),
while pointer aliasing is UB. And here's UB in action:
muntyan@munt10:/tmp$ cat alias.c
#include <stdio.h>
int func (void)
{
int f = 9;
*(short*)&f = 2;
return f;
}
int main (void)
{
printf ("%d\n", func());
return 0;
}
muntyan@munt10:/tmp$ gcc -O2 -Wall alias.c
alias.c: In function 'func':
alias.c:6: warning: dereferencing type-punned pointer will break
strict-aliasing rules
muntyan@munt10:/tmp$ ./a.out
9
Yevgen
.
- References:
- Neatest way to get the end pointer?
- From: Tomás Ó hÉilidhe
- Re: Neatest way to get the end pointer?
- From: Tomás Ó hÉilidhe
- Re: Neatest way to get the end pointer?
- From: Old Wolf
- Re: Neatest way to get the end pointer?
- From: Tomás Ó hÉilidhe
- Re: Neatest way to get the end pointer?
- From: Chris Thomasson
- Re: Neatest way to get the end pointer?
- From: Richard Bos
- Neatest way to get the end pointer?
- Prev by Date: Re: Dereference an array pointer... UB?
- Next by Date: Re: A solution for the allocation failures problem
- Previous by thread: Re: Neatest way to get the end pointer?
- Next by thread: Re: Neatest way to get the end pointer?
- Index(es):
Relevant Pages
|