From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18561 invoked by alias); 18 Oct 2007 10:48:02 -0000 Received: (qmail 18548 invoked by uid 22791); 18 Oct 2007 10:48:02 -0000 X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (66.187.233.31) by sourceware.org (qpsmtpd/0.31) with ESMTP; Thu, 18 Oct 2007 10:48:00 +0000 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx1.redhat.com (8.13.8/8.13.1) with ESMTP id l9IAlvRh017178 for ; Thu, 18 Oct 2007 06:47:58 -0400 Received: from zebedee.littlepinkcloud.COM (vpn-14-33.rdu.redhat.com [10.11.14.33]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id l9IAlsHA025280; Thu, 18 Oct 2007 06:47:55 -0400 Received: from littlepinkcloud.COM (localhost.localdomain [127.0.0.1]) by zebedee.littlepinkcloud.COM (8.13.8/8.13.5) with ESMTP id l9IAlqmA001483; Thu, 18 Oct 2007 11:47:53 +0100 Received: (from aph@localhost) by littlepinkcloud.COM (8.13.8/8.13.5/Submit) id l9IAlpTQ001480; Thu, 18 Oct 2007 11:47:51 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <18199.14807.784370.382284@zebedee.pink> Date: Fri, 19 Oct 2007 02:54:00 -0000 From: Andrew Haley To: Michael Haubenwallner Cc: gcc-help@gcc.gnu.org Subject: Re: ignore that strict-aliasing warning ? In-Reply-To: <1192695762.13354.50.camel@sapc154> References: <1192695762.13354.50.camel@sapc154> X-Mailer: VM 7.19 under Emacs 22.0.93.1 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: 2007-10/txt/msg00233.txt.bz2 Michael Haubenwallner writes: > $ cat test.c > /* 1*/ extern void allocate(int size, void** p); > /* 2*/ > /* 3*/ typedef struct X { > /* 4*/ char A; > /* 5*/ } X; > /* 6*/ > /* 7*/ X* test1(void) > /* 8*/ { > /* 9*/ X *x; > /*10*/ allocate((int)sizeof(X), (void**)&x); /* current code */ > /*11*/ allocate((int)sizeof(X), &x); /* as older gcc warned here already */ > /*12*/ return x; > /*13*/ } > /*14*/ > /*15*/ X* test2(void) > /*16*/ { > /*17*/ union { > /*18*/ X *x; > /*19*/ void* v; > /*20*/ } u; > /*21*/ allocate((int)sizeof(X), &u.v); > /*22*/ allocate((int)sizeof(X), (void**)&u); > /*23*/ return u.x; > /*24*/ } > $ gcc -Wall -O2 -c test.c -Wstrict-aliasing=2 > test.c: In function 'test1': > test.c:10: warning: dereferencing type-punned pointer will break strict-aliasing rules > test.c:11: warning: passing argument 2 of 'allocate' from incompatible pointer type > test.c: In function 'test2': > test.c:22: warning: dereferencing type-punned pointer might break strict-aliasing rules > > Strange is that line 10 "will", but line 22 "might" break > strict-aliasing rules. I don't see what's strange about it. Assigning to u.v and then converting the address of u to a void** is perfectly legal C. It's a pointless thing to do, though, and rather than this you might as well do Line 21. > Do I really need the union here like in test2() and line 21 ? There's no way for us to tell. > Or is there another "correct" way to get rid of warning in line 10 ? If you could tell us what you're really trying to do we might be able to help. The code in Line 10 makes no sense at all: it has no meaning, and can't possibly do anything useful. The sensible way to do what you need is simply: void *p; allocate((int)sizeof(X), &p); X *x = (x*)p; Andrew.