Army1987 said:
"Keith Thompson" <
[email protected]> ha scritto nel messaggio
Argh, you're right. My comment about n1124 6.2.6.1p6 do not apply to
the above example.
They do, however, apply to the following:
struct S
{
char s[10];
};
struct S s;
struct S t;
strcpy(s.s, "hello");
t = s;
(Assume that this appears inside a function definition, and that a
"#include <string.h>" is visible.)
I think on a system where char is signed, and the equivalent of the
most negative integer (for two's complement) or negative zero (for
ones' complement or sign and magnitude) is a trap, and s[8]
happened to be such a representation...
Ok, let's consider a perhaps more realistic example (characters
introduce other issues).
#define MAX_VECTOR_LEN 100
struct int_vector {
int len;
int vec[MAX_VECTOR_LEN];
};
int main(void)
{
struct int_vector v1, v2;
v1.len = 3;
v1.vec[0] = 10;
v1.vec[1] = 20;
v1.vec[2] = 30;
v2 = v1;
return 0;
}
v1.vec contains 3 initialized int element, and 97 uninitialized int
elements. The assignment "v2 = v1;" copies all 100 elements.
The new wording in n1124 makes it clear that structures don't have
trap representations, so the assignment is well defined, even though
the final values of the uninitialized elements are not. (I don't
think it's even clear that v1.vec[50] == v2.vec[50].)
In the original C99, though, since the code is accessing uninitialized
objects, it could invoke undefined behavior. Even in C90, which
didn't have the explicit concept of trap representations, accessing an
uninitialized int still invokes UB. Or consider the same code with
arrays of a floating-point or pointer type.
Now I'd be surprised if any real-world C90 or C99 implementation
actually had a problem with this. I doubt that any real compiler
would implement struct assignment using lower-level operations that
can trap on bad data; it's much easier to do the equivalent of calling
memcpy without worrying about the contents. I'm sure that's why the
committee felt free to make this change in TC2, which was incorporated
into N1124.
But the DS9K compiler probably does something really nasty with this
code unless you specifically invoke it in N1124-compliant mode.