christian.bau said:
On Mar 16, 3:51 am, Tim Rentsch <
[email protected]> wrote:
This argument is wrong, because of 6.5.16.1 p3. Partial overlap
in such cases is specifically undefined behavior. Therefore
the assumption that *a = *c won't affect the value of *c is
a priori conforming and needs no further justification.
You just gave exactly the further justification that you claim isn't
needed.
void f (int* p, int* q)
{
char tmp1 [sizeof (int)];
char tmp2 [sizeof (int)];
*p = 1;
memcpy (tmp1, p, sizeof (int));
*q = 2;
memcpy (tmp2, p, sizeof (int));
if (memcmp (tmp1, tmp2, sizeof (int)) != 0)
printf ("Can this happen?\n");
}
[indentation added]
The printf can happen on some implementations without any undefined
behaviour involved.
This example is irrelevant to the point under discussion. Do you
not understand the difference between
*q = 2;
and
*q = *p;
? Of course it is possible that the first of these assignments
can change the value of *p (in the context of the code sample,
and stipulating no undefined behavior). The second cannot.
You are of course wrong. On an implementation where the type "int" has
no alignment restriction, I can write
char myarray [sizeof (int) + 1];
int* p = (int *) &myarray [0];
int* q = (int *) &myarray [1];
*p = 1;
*q = *p;
On a common implementation where sizeof (int) == 4, the first
assignment sets the bytes in myarray to
1, 0, 0, 0, unknown (littleendian) or 0, 0, 0, 1, unknown
(bigendian).
The second assignment sets the bytes to
1, 1, 0, 0, 0 (littleendian) or 0, 0, 0, 0, 1 (bigendian).
In either case the second assignment modifies the four bytes that p
points to. Without any undefined behaviour up to this point.