M
Marcel van Kervinck
Dear all,
I would like to confirm my suspicion of a compiler
bug in gcc4 for MacOSX. The code example below expects
that the initializer of 'v' sets all elements in
v.u.bytes[] to zero, as specified by the C99 standard:
"""21 If there are fewer initializers in a brace-enclosed
list than there are elements or members of an
aggregate, [...] the remainder of the aggregate shall
be initialized implicitly the same as objects that
have static storage duration."""
(ref: ANSI+ISO+IEC+8999-1999, 6.7.8 Initialization, p.127)
However, the code only works as expected on some platforms:
system cpu compiler result
------ --- -------- ------
SunOS-5.8 UltraSPARC-IIe gcc-2.95.3 returns OK
FreeBSD-4.11 Pentium2 gcc-2.95.4 returns OK
SunOS-5.8 UltraSPARC-IIi gcc-3.1.1 returns OK
Darwin-7.9.0 PowerPC-G4 gcc-3.3 returns OK
Linux-2.4.29 Celeron gcc-3.3.5 returns OK
FreeBSD-5.4 Pentium4 gcc-3.4.2 returns OK
Darwin-8.0.0 PowerPC-G4 gcc-4.0.0 failed assertion `v.u.bytes[1] == 0'
Darwin-8.0.0 PowerPC-G5 gcc-4.0.0 failed assertion `v.u.bytes[1] == 0'
Interestingly, removing the intermediate
level 'union u' makes the code pass.
The questions I'm seeking an answer to:
1. Is this a compiler bug or a display of undefined behaviour?
2. Is this gcc4-specific or only in combination with PowerPC?
Best regards,
Marcel
-- _ _
_| |_|_|
|_ |_ Marcel van Kervinck
|_| (e-mail address removed)
----------------------------------------------------------------
#include <assert.h>
typedef struct {
union {
unsigned char bytes[2];
} u;
} vector_t;
int main(void)
{
int i;
for (i=0; i<2; i++) {
vector_t v = {{{0,}}};
assert(v.u.bytes[1] == 0);
v.u.bytes[1]++;
}
}
----------------------------------------------------------------
I would like to confirm my suspicion of a compiler
bug in gcc4 for MacOSX. The code example below expects
that the initializer of 'v' sets all elements in
v.u.bytes[] to zero, as specified by the C99 standard:
"""21 If there are fewer initializers in a brace-enclosed
list than there are elements or members of an
aggregate, [...] the remainder of the aggregate shall
be initialized implicitly the same as objects that
have static storage duration."""
(ref: ANSI+ISO+IEC+8999-1999, 6.7.8 Initialization, p.127)
However, the code only works as expected on some platforms:
system cpu compiler result
------ --- -------- ------
SunOS-5.8 UltraSPARC-IIe gcc-2.95.3 returns OK
FreeBSD-4.11 Pentium2 gcc-2.95.4 returns OK
SunOS-5.8 UltraSPARC-IIi gcc-3.1.1 returns OK
Darwin-7.9.0 PowerPC-G4 gcc-3.3 returns OK
Linux-2.4.29 Celeron gcc-3.3.5 returns OK
FreeBSD-5.4 Pentium4 gcc-3.4.2 returns OK
Darwin-8.0.0 PowerPC-G4 gcc-4.0.0 failed assertion `v.u.bytes[1] == 0'
Darwin-8.0.0 PowerPC-G5 gcc-4.0.0 failed assertion `v.u.bytes[1] == 0'
Interestingly, removing the intermediate
level 'union u' makes the code pass.
The questions I'm seeking an answer to:
1. Is this a compiler bug or a display of undefined behaviour?
2. Is this gcc4-specific or only in combination with PowerPC?
Best regards,
Marcel
-- _ _
_| |_|_|
|_ |_ Marcel van Kervinck
|_| (e-mail address removed)
----------------------------------------------------------------
#include <assert.h>
typedef struct {
union {
unsigned char bytes[2];
} u;
} vector_t;
int main(void)
{
int i;
for (i=0; i<2; i++) {
vector_t v = {{{0,}}};
assert(v.u.bytes[1] == 0);
v.u.bytes[1]++;
}
}
----------------------------------------------------------------