From owner-freebsd-current Mon Jul 15 14:14:44 2002 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 314F737B400; Mon, 15 Jul 2002 14:14:42 -0700 (PDT) Received: from harrier.mail.pas.earthlink.net (harrier.mail.pas.earthlink.net [207.217.120.12]) by mx1.FreeBSD.org (Postfix) with ESMTP id BB8E843E65; Mon, 15 Jul 2002 14:14:41 -0700 (PDT) (envelope-from tlambert2@mindspring.com) Received: from pool0196.cvx21-bradley.dialup.earthlink.net ([209.179.192.196] helo=mindspring.com) by harrier.mail.pas.earthlink.net with esmtp (Exim 3.33 #1) id 17UDB1-0004ur-00; Mon, 15 Jul 2002 14:14:39 -0700 Message-ID: <3D333B0E.439AA056@mindspring.com> Date: Mon, 15 Jul 2002 14:13:50 -0700 From: Terry Lambert X-Mailer: Mozilla 4.79 [en] (Win98; U) X-Accept-Language: en MIME-Version: 1.0 To: Peter Edwards Cc: luigi@freebsd.org, mike@freebsd.org, current@freebsd.org Subject: Re: different packing of structs in kernel vs. userland ? References: <200207150645.17u4iB1Lrq3Nl3qG0@hazard.mail.atl.earthlink.net> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-current@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG Peter Edwards wrote: > > He's making the valid point that for: > > > > struct foo *fee; > > > > It's possible that: > > > > sizeof(struct foo) != (((char *)&fee[1]) - ((char *)&fee[0])) > > Wouldn't that mean > > .. struct X *xarr = malloc(sizeof (struct X) * arrayLen); > > wouldn't produce a useable array of struct X of length arrayLen? > That can't be right. No, it doesn't mean that, because there *is* end padding in the size calculation. That's kind of the whole point of this thread. In the directory entry example, the last element is a d_name[1] that's an overlay array that's much longer than it appears (it is always at least 4 bytes). This is because it's meant to work with other than C99 (which permits an array with a declared size of 0). It's necessary for the code to know the offset of the start of that elemenet from the start of the structure, and that offset is *NOT* "sizeof(struct) - 1", as you would naievely believe, as if there were no end padding, because the total size of the structure is not always going to be evenly divisible by the number of bytes between alignment boundaries. Does this make more sense?!? -- Terry To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-current" in the body of the message