From owner-freebsd-hackers Sun Mar 3 10: 0:42 2002 Delivered-To: freebsd-hackers@freebsd.org Received: from maile.telia.com (maile.telia.com [194.22.190.16]) by hub.freebsd.org (Postfix) with ESMTP id 64C9B37B41A for ; Sun, 3 Mar 2002 10:00:33 -0800 (PST) Received: from d1o913.telia.com (d1o913.telia.com [195.252.44.241]) by maile.telia.com (8.11.6/8.11.6) with ESMTP id g23I0W415833 for ; Sun, 3 Mar 2002 19:00:32 +0100 (CET) Received: from falcon.midgard.homeip.net (h217n1fls20o913.telia.com [212.181.162.217]) by d1o913.telia.com (8.8.8/8.8.8) with SMTP id TAA14278 for ; Sun, 3 Mar 2002 19:00:30 +0100 (CET) Received: (qmail 56081 invoked by uid 1001); 3 Mar 2002 18:00:30 -0000 Date: Sun, 3 Mar 2002 19:00:30 +0100 From: Erik Trulsson To: Ian Cc: freebsd-hackers Subject: Re: A few questions about a few includes Message-ID: <20020303180029.GA56041@student.uu.se> Mail-Followup-To: Ian , freebsd-hackers References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.3.27i Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Sun, Mar 03, 2002 at 10:27:17AM -0700, Ian wrote: > > > > In : > > > > /* > > * pargs, used to hold a copy of the command line, if it had a sane > > * length > > */ > > struct pargs { > > u_int ar_ref; /* Reference count */ > > u_int ar_length; /* Length */ > > u_char ar_args[0]; /* Arguments */ > > }; > > > > This does indeed seem to make little or no sense. Could someone explain > > this? Is ar_args supposed to be a pointer or what? > > This is a common technique for defining a structure which is some > descriptive information about an array of objects is followed by an > open-ended array of those objects. (In this case the "objects" are > characters.) The ar_args member of the structure gives a name to that > location in the structure without reserving any space (and thus when the > technique is used, there can only ever be one [0] member and it must be at > the end of the structure). You access the open-ended array of objects just > as you would any other array embedded within a structure, E.G. > instance->ar_args[n]. > > Not all compilers support defining zero-length arrays like this. And that's > a pity; it's an incredibly useful technique, and the alternatives to it are > not nearly as elegant and generally involve ugly recasting of pointers. For those compilers that don't support zero-length arrays one can still use the same trick but with a one-element array at the end of the struct. One just has to remember to that element into account when allocating memory for the structure. Slightly uglier, but not much. It might be worth mentioning that this trick is not actually allowed according to the C standard and in principle invokes undefined behaviour. OTOH, AFAIK the trick does work on all existing compilers, so while it is not standard-conforming it is quite portable. -- Erik Trulsson ertr1013@student.uu.se To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message