Re: Uninitialized memory, malloc and unsigned char
- From: "google@xxxxxxxxxxxxx" <google@xxxxxxxxxxxxx>
- Date: Fri, 30 May 2008 03:21:19 -0700 (PDT)
On May 30, 7:24 am, Jun Woong <wo...@xxxxxxxxx> wrote:
lawrence.jo...@xxxxxxxxxxx wrote:
Jun Woong <wo...@xxxxxxxxx> wrote:
I'm surprised, however, to see the last sentence quoted above. That
seems to say that an implementation is prohibited from incurring
undefined behavior even when the member of a struct being assigned
has a trap representation and it does the member-to-member
assignment. Is this really intended, or does the sentence in question
have to read as
The value of a structure or union object is not allowed to have a
trap representation unless one of its members has a trap
representation.
It is really intended -- structs and unions do not have trap
representations. See DR #222, which is what motivated that sentence.
Thanks for the reference. It was why I felt unfamiliar with the
wording that it was added by TC2!
I agree that such a wording has to be there given many existing
constructs using partially-initialized structures, even if I tried to
fully initialize them even when only used in part.
Now what I wonder is, if the committee really intended to allow an
erroneous code like
struct {
/* members whose types allow a trap representation */
} foo, bar;
foo = bar;
to be strictly conforming.
I think small touch could improve that new wording to preclude this
anomaly.
I don't think this matters if we accept that unsigned char cannot trap
in any circumstances.
All the standard does is require that structure members be copied by
something analogous to memcpy rather than member assignment.
But it's fairly obvious that it has to work like this anyway.
union u {
double d;
unsigned char c[sizeof(double)];
};
void func(void) {
union u x,y;
memcpy(&y.c, &double_trap_representation, sizeof y.c);
union_assignment_in_different_compilation_unit(&x, &y);
}
union_assignment_in_different_compilation_unit(&x, &y) {
*x = *y;
}
Unless the union is going to be required to carry additional bits
around with it signaling the valid members then I don't think there's
any way the compiler can be aware which member(s) to use to do the
assignment.
Note that my earlier impression of what the standard was saying with
regard to uninitialized bytes is not incompatible with this.
Tim.
.
- References:
- Uninitialized memory, malloc and unsigned char
- From: Tim Woodall
- Re: Uninitialized memory, malloc and unsigned char
- From: Jun Woong
- Re: Uninitialized memory, malloc and unsigned char
- From: Jun Woong
- Uninitialized memory, malloc and unsigned char
- Prev by Date: Re: Uninitialized memory, malloc and unsigned char
- Previous by thread: Re: Uninitialized memory, malloc and unsigned char
- Index(es):
Relevant Pages
|