Re: Pointer equality and dereferencing
- From: "Wojtek Lerch" <Wojtek_L@xxxxxxxx>
- Date: Thu, 7 Sep 2006 10:26:51 -0400
"Philip Potter" <philip.potter@xxxxxxxxxx> wrote in message news:edot9h$9h21@xxxxxxxxxxxxxxxxxxxxxxx
"Wojtek Lerch" <Wojtek_L@xxxxxxxx> wrote in message
news:4m9krrF56aggU1@xxxxxxxxxxxxxxxxx
"Skarmander" <invalid@xxxxxxxxxxxxxx> wrote in message
news:44ff4275$0$4530$e4fe514c@xxxxxxxxxxxxxxxxx
> Wojtek Lerch wrote:
How about this very simple example:
struct twoptrs { int *a, *b; };
void twoptrscpy( struct twoptrs *dst, struct twoptrs const *src ) {
if ( memcmp( &src->a, &src->b, sizeof(src->a) ) )
memcpy( dst, src, sizeof(*dst) );
else {
memcpy( &dst->a, &src->b, sizeof(dst->a) );
memcpy( &dst->b, &src->a, sizeof(dst->b) );
}
}
int main( void ) {
int a=0, b;
struct twoptrs x = { &a, &b+1 }, y;
twoptrscpy( &y, &x );
return *y.a;
}
Somewhat bizarrely I think this is undefined, if and only if &a == &b+1.
Even though the values are equal, you are dereferencing a pointer which is
based on b (since it came from src->b) and you shouldn't have, because it's
one-past-the-end.
However I don't think this really matters in the real world. In the above
structure, there is no semantic meaning to the struct twoptrs.
I have no idea what you mean by "semantic meaning". It's a structure that contains two pointers. The pointers have meaningless names and serve no useful purpose in this particular program other than to illustrate my point; but that's because any real-life example would be a lot bigger and more complicated and nobody would bother to read it.
I can't see a situation where you'd ever compare a one-past-the-end with a
pointer-to-first from a different array.
No, but I can think of situations where you'd compare bytes of various objects and perform arithmetic on them, without paying attention to the type or meaning of the values that the bytes represent. That's how a lot of compression or encryption algorithm works. If you have a complicated algorithm that takes the a big object and attempts to reconstruct its value in another big object using some convoluted algorithm, is it sufficient to prove that the sequences of byte values in the two objects are the same, or are you also required to track down the origin of each byte and prove that it came from the same spot in the original object?
Your example above loses all semantic meaning about the pointers when they
are entered into struct twoptrs. There is no obvious contract or invariant
which you try to maintain. Why does your function arbitrarily swap two
pointers which may point into different arrays? Why do you put two pointers
with different offsets into the struct?
Who cares? It's just a silly little program. It's not supposed to be an example of good programming technique. It's supposed to be an example that demonstrates that the C standard is not clear on whether the behaviour of certain programs is defined or not. There's no reason why the C standard doesn't apply to silly little programs, is it?
.
- References:
- Pointer equality and dereferencing
- From: Andrea
- Re: Pointer equality and dereferencing
- From: Jun Woong
- Re: Pointer equality and dereferencing
- From: Wojtek Lerch
- Re: Pointer equality and dereferencing
- From: Skarmander
- Re: Pointer equality and dereferencing
- From: Wojtek Lerch
- Re: Pointer equality and dereferencing
- From: Philip Potter
- Pointer equality and dereferencing
- Prev by Date: Re: Pointer equality and dereferencing
- Next by Date: Re: Typdef pointers to structs or not? [from clc]
- Previous by thread: Re: Pointer equality and dereferencing
- Next by thread: Re: Pointer equality and dereferencing
- Index(es):
Relevant Pages
|
Loading