From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1505 invoked by alias); 8 Sep 2010 10:02:16 -0000 Received: (qmail 1457 invoked by uid 48); 8 Sep 2010 10:02:05 -0000 Date: Wed, 08 Sep 2010 10:02:00 -0000 Message-ID: <20100908100205.1456.qmail@sourceware.org> X-Bugzilla-Reason: CC References: Subject: [Bug middle-end/43976] warning about increased alignment during casting printed even though variable is properly aligned In-Reply-To: Reply-To: gcc-bugzilla@gcc.gnu.org To: gcc-bugs@gcc.gnu.org From: "ibolton at gcc dot gnu dot org" Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2010-09/txt/msg01013.txt.bz2 ------- Comment #5 from ibolton at gcc dot gnu dot org 2010-09-08 10:02 ------- Confirmed on latest 4.4, 4.5 and 4.6 (trunk). Related GCC documentation on alignment of structure fields is here: http://gcc.gnu.org/onlinedocs/gcc-4.5.0/gcc/Variable-Attributes.html#Variable-Attributes In the short-term, one workaround is to write the code as follows: #include struct Foo { char c[sizeof(int)]; } __attribute__((aligned(4))); char junk; Foo f; int main() { int *i = reinterpret_cast(&f); *i = 0x44434241; printf("%c %c %c %c", f.c[0], f.c[1], f.c[2], f.c[3]); } By aligning the structure Foo to 4 bytes, you can successfully cast a Foo* to an int* and then initialise all four chars in one go. (Without the type attribute for the struct Foo, you still get the warning.) My example prints "A B C D". FYI: I have tracked down the alleged offending code mentioned in an earlier comment to build_c_cast() in c-typeck.c. -- ibolton at gcc dot gnu dot org changed: What |Removed |Added ---------------------------------------------------------------------------- Status|UNCONFIRMED |NEW Ever Confirmed|0 |1 Known to fail| |4.4.5 4.5.2 4.6.0 Last reconfirmed|0000-00-00 00:00:00 |2010-09-08 10:02:04 date| | http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43976