From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24156 invoked by alias); 30 May 2011 12:47:55 -0000 Received: (qmail 24147 invoked by uid 22791); 30 May 2011 12:47:55 -0000 X-SWARE-Spam-Status: No, hits=-6.6 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_HI,SPF_HELO_PASS,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 30 May 2011 12:47:40 +0000 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p4UCldcs015057 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 30 May 2011 08:47:39 -0400 Received: from zebedee.pink (ovpn-113-62.phx2.redhat.com [10.3.113.62]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p4UClcAX022988; Mon, 30 May 2011 08:47:39 -0400 Message-ID: <4DE391E9.2090609@redhat.com> Date: Mon, 30 May 2011 18:47:00 -0000 From: Andrew Haley User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110428 Fedora/3.1.10-1.fc14 Thunderbird/3.1.10 MIME-Version: 1.0 To: gcc-help@gcc.gnu.org Subject: Re: Weird strict-aliasing break References: In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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/msg00425.txt.bz2 On 05/30/2011 01:33 PM, Thibault Castel wrote: >> ---------- Message transféré ---------- >> From: Steffen Dettmer >> To: gcc-help@gcc.gnu.org >> Date: Mon, 30 May 2011 13:59:51 +0200 >> Subject: Re: Weird strict-aliasing break >> On Mon, May 30, 2011 Thibault Castel wrote: >>> 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... >> >> Beside that this may have other disadvantages, such as depending >> on byte order, padding and alighnment issues, in general you >> cannot assume that constructions like: >> >> struct s { >> int a; // or int32_t >> }; >> char buffer[]; >> >> struct s *p = buffer; p->a; >> struct s *p = buffer+1; p->a; >> >> this even may crash (we had such problems on some ARM CPUs). >> > > Yes, we have some ARM CPUs as targets but specific code (with such > aligment adaptations) are used. The code I provided is x86 targeted > only ;) > >> I think better here is using memcpy, but still "you have to know >> what you are doing". > > No, because I don't want to copy anything. I just need to access > fields for reading and extracting data. I don't write into it. It's > really just a memory mapping. You're still better off with memcpy. There's no loss of efficiency, and it'll be legal C++. Andrew.