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 Message-ID: <19970810100337.ZV59622@uriah.heep.sax.de> In-Reply-To: <199708100722.DAA03236@skynet.ctr.columbia.edu>; from Bill Paul on Aug 10, 1997 03:22:28 -0400 References: <199708100722.DAA03236@skynet.ctr.columbia.edu>
next in thread | previous in thread | raw e-mail | index | archive | help
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. ;-)
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19970810100337.ZV59622>