From owner-freebsd-current@FreeBSD.ORG Wed Sep 8 12:06:42 2004 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 8789016A4D6 for ; Wed, 8 Sep 2004 12:06:42 +0000 (GMT) Received: from n33.kp.t-systems-sfr.com (n33.kp.t-systems-sfr.com [129.247.16.33]) by mx1.FreeBSD.org (Postfix) with ESMTP id A8ACB43D49 for ; Wed, 8 Sep 2004 12:06:41 +0000 (GMT) (envelope-from harti@freebsd.org) Received: from n81.sp.op.dlr.de (n81g.sp.op.dlr.de [129.247.163.1]) i88C6U2388160; Wed, 8 Sep 2004 14:06:30 +0200 Received: from zeus.nt.op.dlr.de (zeus.nt.op.dlr.de [129.247.173.3]) i88C6TI36308; Wed, 8 Sep 2004 14:06:29 +0200 Received: from beagle.kn.op.dlr.de (opkndnwsbsd178 [129.247.173.178]) by zeus.nt.op.dlr.de (8.11.7+Sun/8.9.1) with ESMTP id i88C6Se01249; Wed, 8 Sep 2004 14:06:28 +0200 (MET DST) Date: Wed, 8 Sep 2004 14:06:31 +0200 (CEST) From: Harti Brandt X-X-Sender: brandt@beagle.kn.op.dlr.de To: Pavlin Radoslavov In-Reply-To: <200409072032.i87KWVwK035940@possum.icir.org> Message-ID: <20040908140335.R23565@beagle.kn.op.dlr.de> References: <200409072032.i87KWVwK035940@possum.icir.org> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII cc: Craig Rodrigues cc: freebsd-current@freebsd.org Subject: Re: g++ may fail to compile __packed structures X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: Harti Brandt List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Sep 2004 12:06:42 -0000 On Tue, 7 Sep 2004, Pavlin Radoslavov wrote: PR>> On Mon, Sep 06, 2004 at 01:24:15PM -0700, Pavlin Radoslavov wrote: PR>> > It appears that the lastest g++ compiler that comes with FreeBSD may PR>> > fail to compile a __packed structure when one of its fields is PR>> > passed by reference. At the end of this email is a sample program PR>> > that demonstrates the problem. The compilation error is: PR>> > PR>> > pavlin@carp[14] g++34 test.cc PR>> > test.cc: In function `int main()': PR>> > test.cc:22: error: cannot bind packed field `f1.foo::f' to `int&' PR>> > Exit 1 PR>> > PR>> > The problem appears to exist only with the recent g++ compiler that PR>> > comes with FreeBSD: PR>> PR>> This change was made recently to gcc: PR>> http://gcc.gnu.org/ml/gcc-patches/2003-07/msg01664.html PR> PR>Yes, I am aware of that patch (when I did some search on the subject PR>before posting my email). However, again, when I use the vanilla g++ PR>3.4.1 which is 2 months old, or even the vanilla 3.4.2 which was PR>just released today, I don't get the compilation error. PR>Hence, why the disparity between the vanilla gcc and the lastest PR>gcc that comes with FreeBSD? PR> PR>> Apparently in C++, you are not allowed to have non-const references PR>> to packed fields. See: PR>> http://www.comnets.rwth-aachen.de/doc/c++std/decl.html#dcl.init.ref PR> PR>The above URL doesn't say anything about packed fields. PR>Please correct me if I am wrong, but I think that __packed is not PR>part of the C or C++ standart, hence this leaves some gray area for PR>interpretation. Anyway, this is a subject for the gcc ML... PR> PR>However, I am trying to find-out why the FreeBSD gcc behaves PR>different from the vanilla gcc, and which compiler has the "right" PR>behavior. Neither. As you said __packed is not part of any standard. If you take into account that a reference is essentially a pointer (at least in the case of a function argument) this pointer could easily have the wrong alignment, because your packed field may have the wrong alignment. To catch this case the compiler would need to propagate that information at run-time to the function. In short: don't use such features. If you need packed (because you're accessing hardware) pass by value not by reference. harti