Here's another bug but with lccwin64:
#include <stdio.h>"
#include <stdlib.h>"
#include <stdint.h>"
typedef struct {
int32_t a;
int32_t b;
union {
int64_t c;
struct {
int32_t clow;
int32_t chigh;
};
};
} S;
int main(void) {
printf(".c offset: %d\n", offsetof(S,c));
printf(".clow offset: %d\n", offsetof(S,clow));
printf(".chigh offset: %d\n", offsetof(S,chigh));
}
'c' should have offset 8, and 'clow' and 'chigh' should have offsets of
8 and 12. But lccwin64 reports them as 16 and 20.
It's OK with lccwin32 (and with other 32 or 64-bit compilers I've tried).
lccwin64 version is June 03 2016.
Here's another bug but with lccwin64:
#include <stdio.h>"
#include <stdlib.h>"
#include <stdint.h>"
typedef struct {
int32_t a;
int32_t b;
union {
int64_t c;
struct {
int32_t clow;
int32_t chigh;
};
};
} S;
int main(void) {
printf(".c offset: %d\n", offsetof(S,c));
printf(".clow offset: %d\n", offsetof(S,clow));
printf(".chigh offset: %d\n", offsetof(S,chigh));
}
'c' should have offset 8, and 'clow' and 'chigh' should have offsets of
8 and 12. But lccwin64 reports them as 16 and 20.
It's OK with lccwin32 (and with other 32 or 64-bit compilers I've tried).
lccwin64 version is June 03 2016.
BartC <bc@freeuk.com> writes:
Here's another bug but with lccwin64:
#include <stdio.h>"
#include <stdlib.h>"
#include <stdint.h>"
typedef struct {
int32_t a;
int32_t b;
union {
int64_t c;
struct {
int32_t clow;
int32_t chigh;
};
};
} S;
int main(void) {
printf(".c offset: %d\n", offsetof(S,c));
printf(".clow offset: %d\n", offsetof(S,clow));
printf(".chigh offset: %d\n", offsetof(S,chigh));
}
'c' should have offset 8, and 'clow' and 'chigh' should have offsets of
8 and 12. But lccwin64 reports them as 16 and 20.
BartC, does that compile? Ignoring the extra quotation marks on
the #include directives, offsetof() is defined in <stddef.h>,
not <stdlib.h>. A reference to offsetof() without `#include
<stddef.h>` should at least be diagnosed at compile time, and
preferably rejected.
The actual offsets are implementation-defined due to padding, but
assuming CHAR_BIT==8 I agree that 8 is the only reasonable value
for offsetof(S, c) and offsetof(S, clow).
I note that unnamed structs and unions, which this example depends
on, are supported only in C11, not C90 or C99.
current status of lcc-win's C11 support. The issue can easily be
avoided by naming the union and the struct.
I offer an alternate version of the test program. It's possible
that naming the nested union and struct might invalidate the test
by working around the problem. I currently have no way to test that.
struct S {
int32_t a;
int32_t b;
union {
int64_t c;
struct {
int32_t clow;
int32_t chigh;
} s;
} u;
};
On 10/10/2016 19:53, Keith Thompson wrote:
BartC <bc@freeuk.com> writes:
Here's another bug but with lccwin64:
#include <stdio.h>"
#include <stdlib.h>"
#include <stdint.h>"
typedef struct {
int32_t a;
int32_t b;
union {
int64_t c;
struct {
int32_t clow;
int32_t chigh;
};
};
} S;
int main(void) {
printf(".c offset: %d\n", offsetof(S,c));
printf(".clow offset: %d\n", offsetof(S,clow));
printf(".chigh offset: %d\n", offsetof(S,chigh));
}
'c' should have offset 8, and 'clow' and 'chigh' should have offsets of
8 and 12. But lccwin64 reports them as 16 and 20.
BartC, does that compile? Ignoring the extra quotation marks on
the #include directives, offsetof() is defined in <stddef.h>,
not <stdlib.h>. A reference to offsetof() without `#include
<stddef.h>` should at least be diagnosed at compile time, and
preferably rejected.
The compilers didn't seem bothered about whether stddef.h was included
or not.
The actual offsets are implementation-defined due to padding, but
assuming CHAR_BIT==8 I agree that 8 is the only reasonable value
for offsetof(S, c) and offsetof(S, clow).
The actual offset isn't important, only that it should be the same for
those two members. But I do rely on the chigh offset being 4 bytes
higher. (The actual struct is below.)
I note that unnamed structs and unions, which this example depends
on, are supported only in C11, not C90 or C99.
I've looked at this before, and anonymous structs and unions seem to be recognised in all the compilers I've tried.
I don't know the
current status of lcc-win's C11 support. The issue can easily be
avoided by naming the union and the struct.
That will break a lot of things, as it means thousands of references
will need the inclusion of an extra, arbitrary member name. But also the original language supports that feature, and I need C to be able to
represent it (alternate representations in C can get very messy).
Since most compilers seem to support the feature, there is no reason not
to use it just because one compiler has a bug in that support.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 406 |
Nodes: | 16 (2 / 14) |
Uptime: | 107:25:01 |
Calls: | 8,526 |
Calls today: | 5 |
Files: | 13,209 |
Messages: | 5,920,295 |