Re: Pointer equality and dereferencing



"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?

.



Relevant Pages

  • Re: How to create an N dimensional array with N elements?
    ... combinations) and for permutations an iterative algorithm is correct. ... pointers and you are repeatedly pushing the same pointer onto your vector, ... int main ...
    (comp.lang.cpp)
  • Re: A taxonomy of types
    ... I am describing how to represent the representation (and, ... if the system follows static typing rules (such as in a compiler), ... so, the hardware sees pointers and pointer arithmetic, but the compiler ... "Besides the char types, up to three sizes of integer, declared short int, ...
    (comp.lang.misc)
  • Re: Malloc code
    ... int xxx; ... As for not using the void pointer, I will have to do some further testing ... I just needed some insight on passing arrays of pointers. ... struct MCB *r1; ...
    (microsoft.public.vc.language)
  • I want my segmentation fault!
    ... no occurrences of free and a lot of routines returning pointers to ... the pointer returned by the allocator (either directly or as a component ... int length_of_list; ...
    (comp.lang.c.moderated)
  • Re: Simple question, err... I think
    ... Your nodes contain no other indication of which pointers are valid, ... struct CountedObject ... int is_red; ... bool lament(char const s) ...
    (comp.programming)

Loading