From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7398 invoked by alias); 30 May 2011 09:20:13 -0000 Received: (qmail 7380 invoked by uid 22791); 30 May 2011 09:20:12 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: sourceware.org Received: from mailout-de.gmx.net (HELO mailout-de.gmx.net) (213.165.64.22) by sourceware.org (qpsmtpd/0.43rc1) with SMTP; Mon, 30 May 2011 09:19:57 +0000 Received: (qmail invoked by alias); 30 May 2011 09:19:56 -0000 Received: from LN-mac29.grenoble.cnrs.fr (EHLO axel) [147.173.67.29] by mail.gmx.net (mp006) with SMTP; 30 May 2011 11:19:56 +0200 Date: Mon, 30 May 2011 12:33:00 -0000 From: Axel Freyn To: gcc-help@gcc.gnu.org Subject: Re: Weird strict-aliasing break Message-ID: <20110530091954.GO4375@axel> Mail-Followup-To: gcc-help@gcc.gnu.org References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.18 (2008-05-17) X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2011-05/txt/msg00418.txt.bz2 Hi Thibault, On Mon, May 30, 2011 at 10:34:40AM +0200, Thibault Castel wrote: > Hi, > > I have a weird warning when compiling my program with g++: > "dereferencing pointer ‘NAME’ does break strict-aliasing rules" > > So, I tried to investigate a little bit and find some clues : I do a > memory mapping from a char buffer to a C struct to have access to > different datas into the buffer. Pretty casual. But, when I try to > access one of my inner field, the warning triggers... Huh... > > To understand a bit more, I wrote a litte test program outside of my > project (really huge). See it in attachment. And I don't really > understand, because the warning is triggered only line 77, not on line > 55. Only when I try to access the inner fields of a mapped structured > included into another structure. > > I successfully avoid the warning with "__attribute__((__may_alias__))" > but this "cheat" bother me because : > - I don't want to fake the compiler > - I need to compile my huge project both under Linux (gcc/g++) and > Windows (Visual IDE) > - This warning is triggered a hundred times in my huge project (old C > project slowly turning into C++ :/) > > I already saw some messages on this list about this warning. But the > difference here is my program IS working. pFoo->a and pFoo->b have > correct values. > > I tried different scenarios > - grow or reduce the raw array > - change the definition order into TData > > [...] > > Thanks a lot for any clue or explanation I'm not 100% sure -- however here is my opinion: - the strict aliasing rules assume that you never access an object through two pointers of different type "at the same time", so your code in line 54 violates strict aliasing, too. - if you violate strict aliasing, that MIGHT introduce problems (especially during optimization, and if you change values). However, it might work without any problems. - gcc warns not about ALL, but only about SOME strict-aliasing violations. You can adjust the warnings by -Wstrict-aliasing=1 (or 2 or 3). With "-Wstrict-aliasing=2", I also obtain the warning warn-strict.cpp:54: warning: dereferencing type-punned pointer will break strict-aliasing rules I think, gcc tries to warn only when it "expects" problems. So probably gcc is "quite sure" that line 54 does not introduce problems (for example gcc 4.7 on x86_64 does not warn at all for your code). - you can avoid all problems with gcc by using "-fno-strict-aliasing", which tells gcc not to assume strict aliasing during it's optimizations: with this option, everything should be safe. I don't know of any portable way how to avoid this possible problem (except correcting the code...) Axel