From owner-freebsd-hackers Tue Mar 20 11:22: 5 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from elfie.org (elfie.org [207.198.61.53]) by hub.freebsd.org (Postfix) with ESMTP id 3F33F37B719 for ; Tue, 20 Mar 2001 11:22:02 -0800 (PST) (envelope-from franklin@elfie.org) Received: (from franklin@localhost) by elfie.org (8.11.0/8.10.2) id f2KJLRD07167; Tue, 20 Mar 2001 14:21:27 -0500 (EST) Date: Tue, 20 Mar 2001 14:21:27 -0500 From: John Franklin To: Peter Seebach Cc: tech-kern@netbsd.org, bsd hackers Subject: Re: Question regarding the array of size 0. Message-ID: <20010320142127.D6167@elfie.org> References: <3AB7A76B.2BCF5D6E@net.com> <200103201903.f2KJ3LO16883@guild.plethora.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.3.9i In-Reply-To: <200103201903.f2KJ3LO16883@guild.plethora.net>; from seebs@plethora.net on Tue, Mar 20, 2001 at 01:03:21PM -0600 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Tue, Mar 20, 2001 at 01:03:21PM -0600, Peter Seebach wrote: > In message <3AB7A76B.2BCF5D6E@net.com>, Shankar Agarwal writes: > >Can someone pls tell me if it is possible to define an array of size 0. > > Not in C. Actually you can (see below). It depends on the compiler and how strict you have it checking things. It only works in this case because the memory manager is allocating an entire page for the structure, not just the size of the structure. It's not uncommon to use this for message-based communications where you have a header and payload and want to use sizeof(struct message) to get the header size, but also want to use foo.payload to access the message itself. In that case, it's more likely to be used as a cast on a buffer, (e.g., ((struct message*) buffer)->payload) Realize, tho, there's a potential portability issue, if you use this. What follows was done on a NetBSD 1.5 system. [24]% cat zero.c && make zero && ./zero #include struct zero_array { int header; int payload[0]; }; int main() { struct zero_array foo; foo.header=1; foo.payload[0]=10; foo.payload[1]=12; printf("Foo:\n\theader: %d\n", foo.header); printf("\tpayload 0: %d\n", foo.payload[0]); printf("\tpayload 1: %d\n", foo.payload[1]); return 0; } cc -O2 -o zero zero.c Foo: header: 1 payload 0: 10 payload 1: 12 jf -- John Franklin franklin@elfie.org ICBM: N37 12'54", W80 27'14" Z+2100' To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message