public inbox for gcc-prs@sourceware.org help / color / mirror / Atom feed
From: manfred@colorfullife.com To: gcc-gnats@gcc.gnu.org Subject: c/9058: structure with flexible array member: offsetof() != sizeof() Date: Thu, 26 Dec 2002 04:36:00 -0000 [thread overview] Message-ID: <20021226123454.27019.qmail@sources.redhat.com> (raw) >Number: 9058 >Category: c >Synopsis: structure with flexible array member: offsetof() != sizeof() >Confidential: no >Severity: serious >Priority: medium >Responsible: unassigned >State: open >Class: wrong-code >Submitter-Id: net >Arrival-Date: Thu Dec 26 04:36:01 PST 2002 >Closed-Date: >Last-Modified: >Originator: Manfred Spraul >Release: gcc (GCC) 3.2 20020903 (RedHat Linux 8.0 3.2-7) >Organization: >Environment: Linux i386 >Description: See the attached test case: struct x {int a; char b; short c[];}; Now sizeof(struct x) is 8, offsetof(struct x, c) is 6. That seems to contradice the C99 standard: section 6.7.2.1, constraint 16. The problem occurs both with gcc and g++ >How-To-Repeat: gcc -o simple simple.c ./simple >Fix: Workarounds: - Do not assume that x_ptr->c == x_ptr+1 - Manually pad structure >Release-Note: >Audit-Trail: >Unformatted: ----gnatsweb-attachment---- Content-Type: application/octet-stream; name="simple.c" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="simple.c" LyoKICogVGVzdGNhc2UgZm9yIGEgcG9zc2libGUgY29tcGlsZXIgYnVnOgogKiBzdHJ1Y3R1cmUg d2l0aCBmbGV4aWJsZSBhcnJheSBtZW1iZXIuCiAqIG9mZnNldG9mKHN0cnVjdCxmbGV4aWJsZV9t ZW1iZXIpICE9IHNpemVvZihzdHJ1Y3QpLgogKgogKiBUaGF0IHNlZW1zIHRvIGJlIGEgdmlvbGF0 aW9uIG9mIDYuNy4yLjEsIGNvbnN0cmFpbnQgMTYgb2YgdGhlIEM5OQogKiBzdGFuZGFyZDoKICog IkZpcnN0LCB0aGUgc2l6ZSBvZiB0aGUgc3RydWN0dXJlIHNoYWxsIGJlIGVxdWFsIHRvIHRoZSBv ZmZzZXQgb2YgdGhlCiAqIG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IHNpemVvZjogJWQsIG9mZnNldG9mOiAlZC5cbiIsIHNpemVvZihzdHJ1 Y3QgbjIpLCBvZmZzZXRvZihzdHJ1Y3QgbjIsIGEpKTsKCXByaW50ZigidmFyOiBzaXplb2Y6ICVk LCBvZmZzZXRvZjogJWQuXG4iLCBzaXplb2Yoc3RydWN0IG4xKSwgb2Zmc2V0b2Yoc3RydWN0IG4x LCBhKSk7CglyZXR1cm4gMDsKfQo=
next reply other threads:[~2002-12-26 12:36 UTC|newest] Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top 2002-12-26 4:36 manfred [this message] 2002-12-26 12:26 Joseph S. Myers 2002-12-26 13:46 Manfred Spraul 2002-12-26 14:26 Joseph S. Myers 2002-12-26 15:46 Manfred Spraul 2003-01-13 0:20 bangerth 2003-01-13 21:13 bangerth
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20021226123454.27019.qmail@sources.redhat.com \ --to=manfred@colorfullife.com \ --cc=gcc-gnats@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).