Date: Tue, 20 Mar 2001 14:21:27 -0500 From: John Franklin <franklin@elfie.org> To: Peter Seebach <seebs@plethora.net> Cc: tech-kern@netbsd.org, bsd hackers <freebsd-hackers@freebsd.org> Subject: Re: Question regarding the array of size 0. Message-ID: <20010320142127.D6167@elfie.org> In-Reply-To: <200103201903.f2KJ3LO16883@guild.plethora.net>; from seebs@plethora.net on Tue, Mar 20, 2001 at 01:03:21PM -0600 References: <3AB7A76B.2BCF5D6E@net.com> <200103201903.f2KJ3LO16883@guild.plethora.net>
next in thread | previous in thread | raw e-mail | index | archive | help
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 <stdio.h> 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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010320142127.D6167>