From owner-freebsd-hackers Sat Jul 13 8:57:58 2002 Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id A5A4B37B400 for ; Sat, 13 Jul 2002 08:57:54 -0700 (PDT) Received: from tom.hrz.tu-chemnitz.de (tom.hrz.tu-chemnitz.de [134.109.132.38]) by mx1.FreeBSD.org (Postfix) with ESMTP id C292243E3B for ; Sat, 13 Jul 2002 08:57:53 -0700 (PDT) (envelope-from ingo.oeser@informatik.tu-chemnitz.de) Received: from tnt158.hrz.tu-chemnitz.de ([134.109.156.158] helo=nightmaster.csn.tu-chemnitz.de ident=root) by tom.hrz.tu-chemnitz.de with esmtp (Exim 4.04) id 17TPHL-0006S7-00; Sat, 13 Jul 2002 17:57:51 +0200 Received: (from ioe@localhost) by nightmaster.csn.tu-chemnitz.de (8.9.1/8.9.1) id RAA19775; Sat, 13 Jul 2002 17:47:55 +0200 Date: Sat, 13 Jul 2002 17:47:55 +0200 From: Ingo Oeser To: "Brian T.Schellenberger" Cc: Yar Tikhiy , hackers@FreeBSD.ORG Subject: Re: Large variables on stack Message-ID: <20020713174755.E758@nightmaster.csn.tu-chemnitz.de> References: <20020712194809.A62768@comp.chem.msu.su> <20020712155951.B4F0BBA05@i8k.babbleon.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2i In-Reply-To: <20020712155951.B4F0BBA05@i8k.babbleon.org>; from bts@babbleon.org on Fri, Jul 12, 2002 at 11:59:51AM -0400 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG Hi, On Fri, Jul 12, 2002 at 11:59:51AM -0400, Brian T.Schellenberger wrote: > Besides, stack allocations are more efficient than heap allocations on every > architecture I know of other than the IBM mainframe. > > Of course, it's is a lot better to dynamically allocate strings of unknown > length than to use large stack buffers, but that's because the dynamic > allocation can be done after you know the length of the string and can avoid > overflows more than because you don't want large stack allocations. You can always do strlen() before and pass that on to your subfunctions. I also like the approach of storing strings together with its length, but is is very un-C. > In a language like ForTran that allows variable-length > automatically-allocated function-scoped items I'd do even those > off of the stack. C99 is also such a language and at least GCC supports it just fine even in scopes. The following (useless) programs demonstrates this: #include #include void print_arg(char *s) { size_t len=strlen(s)+1; { char d[len]; memcpy(&d[0],s,len); printf("%s\n",&d[0]); } } int main(int argc, char **argv) { int i; for (i=0; i