M
Mike S
I came across the following paragraph in the "Semantics" section for
simple assignment in N1124 (C99 draft) and I'm wondering if I'm
interpreting it right:
6.5.16.1p3:
If the value being stored in an object is read from another object that
overlaps in any way
the storage of the first object, then the overlap shall be exact and
the two objects shall
have qualified or unqualified versions of a compatible type; otherwise,
the behavior is
undefined.
The only concievable way for the storage of two objects to overlap in
an assignment context (that I can think of) is in the case where the
new value being stored to an object of type T is the result of the *
operator on an object of type 'pointer to T' (or a pointer to a
different type cast to 'pointer to T'), where the pointer points to a
valid location within the object being assigned to. That's a lot of
verbiage on my part, so hopefully the following code snippet
illustrates what I mean:
/* assume sizeof int >=2 */
int main(void)
{
int i = 10;
int *ip = &i;
i = *ip; /* although pointless, this seems to be allowed \
* because although the storage of i and *ip \
* overlaps, the overlap is exact */
++ip; /* get ready to (potentially) cause some UB */
i = *ip; /* from my understanding, this is UB \
* because the storage of i and *ip \
* overlaps (ip is pointing at a location \
* occupied by the storage of i) \
* but ip is -not- pointing at the lowest \
* addressable byte of i, thus the \
* overlap is not exact and it's undefined */
return 0;
}
DId I read the Standard correctly and is my example illustrative of
what the 6.5.16.1p3 intends to communicate? If not, please set my
straight (and also, if there are other instances where overlap can
occur in an assignment besides trying to assign an object a value from
its own storage, that would be interesting to know, as my example is a
shamelessly contrived one).
simple assignment in N1124 (C99 draft) and I'm wondering if I'm
interpreting it right:
6.5.16.1p3:
If the value being stored in an object is read from another object that
overlaps in any way
the storage of the first object, then the overlap shall be exact and
the two objects shall
have qualified or unqualified versions of a compatible type; otherwise,
the behavior is
undefined.
The only concievable way for the storage of two objects to overlap in
an assignment context (that I can think of) is in the case where the
new value being stored to an object of type T is the result of the *
operator on an object of type 'pointer to T' (or a pointer to a
different type cast to 'pointer to T'), where the pointer points to a
valid location within the object being assigned to. That's a lot of
verbiage on my part, so hopefully the following code snippet
illustrates what I mean:
/* assume sizeof int >=2 */
int main(void)
{
int i = 10;
int *ip = &i;
i = *ip; /* although pointless, this seems to be allowed \
* because although the storage of i and *ip \
* overlaps, the overlap is exact */
++ip; /* get ready to (potentially) cause some UB */
i = *ip; /* from my understanding, this is UB \
* because the storage of i and *ip \
* overlaps (ip is pointing at a location \
* occupied by the storage of i) \
* but ip is -not- pointing at the lowest \
* addressable byte of i, thus the \
* overlap is not exact and it's undefined */
return 0;
}
DId I read the Standard correctly and is my example illustrative of
what the 6.5.16.1p3 intends to communicate? If not, please set my
straight (and also, if there are other instances where overlap can
occur in an assignment besides trying to assign an object a value from
its own storage, that would be interesting to know, as my example is a
shamelessly contrived one).