Date: Sun, 10 Aug 1997 03:22:28 -0400 (EDT) From: Bill Paul <wpaul@skynet.ctr.columbia.edu> To: chat@freebsd.org Subject: variable sized arrays and gcc Message-ID: <199708100722.DAA03236@skynet.ctr.columbia.edu>
next in thread | raw e-mail | index | archive | help
This technically isn't a problem with FreeBSD so I think it's best to
post about it here. Earlier tonight I was looking through a snapshot
of some of the Linux NIS+ client code when I noticed something that
just struck me as really strange. Basically, there was code like the
following:
int some_random_function(str)
char *str;
{
char buf[strlen(str) + 10];
/* do some junk */
return(0);
}
The problem here is that the array 'buf' is being declared with
a variable size. My mommy always told me that you couldn't do that:
buf[10] is okay, buf[strlen(foo) + 10] is not. At first I thought I
was losing my marbles, but it that seems gcc will compile this code.
No other compiler I tried would though (the SunOS 4.1.x unbundled ANSI C
compiler, Solaris 2.5.1 unbundled C compiler, HP-UX 10.20 ANSI C
compiler, SGI IRIX C compiler and the IBM AIX 4.1.x C compiler all
complained about the variable sized array and aborted compilation).
Gcc will not complain about this unless you use -ansi -pedantic:
even then you get a warning, but it compiles the code anyway.
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. (Well, no other UNIX C compiler; I don't
have an M$ C compiler or Borland or Watcom C compiler handy so I
don't know what those do. Elvis only knows what horrors M$ Visual C++
lets you perpetrate.) 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. :)
Is this a side-effect of GNU C and GNU C++ being joined together at
the hip, or did I just miss a memo somewhere?
-Bill
--
=============================================================================
-Bill Paul (212) 854-6020 | System Manager, Master of Unix-Fu
Work: wpaul@ctr.columbia.edu | Center for Telecommunications Research
Home: wpaul@skynet.ctr.columbia.edu | Columbia University, New York City
=============================================================================
"It is not I who am crazy; it is I who am mad!" - Ren Hoek, "Space Madness"
=============================================================================
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199708100722.DAA03236>
