BartC <
[email protected]> 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.
It's OK with lccwin32 (and with other 32 or 64-bit compilers I've tried).
lccwin64 version is June 03 2016.
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. 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.
Regardless of the specific values, all members of a union should
have an offset of 0 relative to the union, and the same offset
relative to an enclosing structure.
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.
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
struct S {
int32_t a;
int32_t b;
union {
int64_t c;
struct {
int32_t clow;
int32_t chigh;
} s;
} u;
};
int main(void) {
printf(".c offset: %zu\n", offsetof(struct S, u.c));
printf(".clow offset: %zu\n", offsetof(struct S, u.s.clow));
printf(".chigh offset: %zu\n", offsetof(struct S, u.s.chigh));
if (offsetof(struct S, u.c) == offsetof(struct S, u.s.clow)) {
puts("ok");
}
else {
puts("failed");
}
}
The expected output is:
.c offset: 8
.clow offset: 8
.chigh offset: 12
ok
Hope this helps.
(godbolt.org provides online access to several versions of clang, gcc,
and icc. Is there a similar site providing online access to lcc-win?)
--
Keith Thompson (The_Other_Keith)
[email protected] <
http://www.ghoti.net/~kst> Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)