From owner-freebsd-chat Sun Aug 10 01:10:59 1997 Return-Path: Received: (from root@localhost) by hub.freebsd.org (8.8.5/8.8.5) id BAA09432 for chat-outgoing; Sun, 10 Aug 1997 01:10:59 -0700 (PDT) Received: from sax.sax.de (sax.sax.de [193.175.26.33]) by hub.freebsd.org (8.8.5/8.8.5) with SMTP id BAA09426 for ; Sun, 10 Aug 1997 01:10:56 -0700 (PDT) Received: (from uucp@localhost) by sax.sax.de (8.6.12/8.6.12-s1) with UUCP id KAA05203 for chat@FreeBSD.ORG; Sun, 10 Aug 1997 10:10:55 +0200 Received: (from j@localhost) by uriah.heep.sax.de (8.8.7/8.8.5) id KAA00731; Sun, 10 Aug 1997 10:03:37 +0200 (MET DST) Message-ID: <19970810100337.ZV59622@uriah.heep.sax.de> Date: Sun, 10 Aug 1997 10:03:37 +0200 From: j@uriah.heep.sax.de (J Wunsch) To: chat@FreeBSD.ORG Subject: Re: variable sized arrays and gcc References: <199708100722.DAA03236@skynet.ctr.columbia.edu> X-Mailer: Mutt 0.60_p2-3,5,8-9 Mime-Version: 1.0 X-Phone: +49-351-2012 669 X-PGP-Fingerprint: DC 47 E6 E4 FF A6 E9 8F 93 21 E0 7D F9 12 D6 4E Reply-To: joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch) In-Reply-To: <199708100722.DAA03236@skynet.ctr.columbia.edu>; from Bill Paul on Aug 10, 1997 03:22:28 -0400 Sender: owner-freebsd-chat@FreeBSD.ORG X-Loop: FreeBSD.org Precedence: bulk As Bill Paul wrote: > Okay, maybe I haven't been paying attention, but this is the first > time I've ever noticed that gcc would let you do this. Personally, > I think it's damn strange, especially since _no_ other C compiler I > can find behaves the same way. [...] > If it's a feature, I don't think it's a > particularly good one since it encourages the use of non-ANSI (and > apparently non-portable) code. If it's a bug, it's got to be the most > carefully engineered bug of all time. :) That's the problem with all local extensions a compiler provides. I think it's an interesting feature, and it's clearly mentioned in the language extensions in the info file: rrays of Variable Length ========================= Variable-length automatic arrays are allowed in GNU C. These arrays are declared like any other automatic arrays, but with a length that is not a constant expression. The storage is allocated at the point of declaration and deallocated when the brace-level is exited. For example: ... There are other extensions that fall into the same class, like zero- length arrays -- could be used as placeholder in a struct where the tail of the struct is not fixed: struct foo { int len; char contents[0]; }; ...where `len' tells you about the actual size of the array. You can then easily malloc() the storage for this struct by var = (struct foo *)malloc(sizeof(struct foo) + actual_length); var->len = actual_length; Of course, if you're going to write portable code, you should not use extensions. But if you're going to write a quick hack for your own, extensions can make your life easier. -- cheers, J"org joerg_wunsch@uriah.heep.sax.de -- http://www.sax.de/~joerg/ -- NIC: JW11-RIPE Never trust an operating system you don't have sources for. ;-)