From owner-freebsd-chat Sun Aug 10 00:22:23 1997 Return-Path: Received: (from root@localhost) by hub.freebsd.org (8.8.5/8.8.5) id AAA07769 for chat-outgoing; Sun, 10 Aug 1997 00:22:23 -0700 (PDT) Received: from skynet.ctr.columbia.edu (skynet.ctr.columbia.edu [128.59.64.70]) by hub.freebsd.org (8.8.5/8.8.5) with SMTP id AAA07761 for ; Sun, 10 Aug 1997 00:22:14 -0700 (PDT) Received: (from wpaul@localhost) by skynet.ctr.columbia.edu (8.6.12/8.6.9) id DAA03236 for chat@freebsd.org; Sun, 10 Aug 1997 03:22:30 -0400 From: Bill Paul Message-Id: <199708100722.DAA03236@skynet.ctr.columbia.edu> Subject: variable sized arrays and gcc To: chat@freebsd.org Date: Sun, 10 Aug 1997 03:22:28 -0400 (EDT) X-Mailer: ELM [version 2.4 PL24] Content-Type: text Sender: owner-freebsd-chat@freebsd.org X-Loop: FreeBSD.org Precedence: bulk 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" =============================================================================