From owner-freebsd-hackers Sat Jul 13 12:16: 7 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 7F39B37B400 for ; Sat, 13 Jul 2002 12:16:04 -0700 (PDT) Received: from mail5.nc.rr.com (fe5.southeast.rr.com [24.93.67.52]) by mx1.FreeBSD.org (Postfix) with ESMTP id 3837143E77 for ; Sat, 13 Jul 2002 12:15:58 -0700 (PDT) (envelope-from bts@fake.com) Received: from sakura.fake.com ([66.26.254.93]) by mail5.nc.rr.com with Microsoft SMTPSVC(5.5.1877.757.75); Sat, 13 Jul 2002 15:14:10 -0400 Received: by sakura.fake.com (Postfix, from userid 111) id 540ECBA05; Sat, 13 Jul 2002 15:13:31 -0400 (EDT) Content-Type: text/plain; charset="iso-8859-1" From: Brian T.Schellenberger To: Ingo Oeser Subject: Re: Large variables on stack Date: Sat, 13 Jul 2002 15:13:30 -0400 X-Mailer: KMail [version 1.3] Cc: Yar Tikhiy , hackers@FreeBSD.ORG References: <20020712194809.A62768@comp.chem.msu.su> <20020712155951.B4F0BBA05@i8k.babbleon.org> <20020713174755.E758@nightmaster.csn.tu-chemnitz.de> In-Reply-To: <20020713174755.E758@nightmaster.csn.tu-chemnitz.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-Id: <20020713191331.540ECBA05@sakura.fake.com> 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 On Saturday 13 July 2002 11:47 am, Ingo Oeser wrote: | 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. Only helpful if you can allocate dynamically-sized arrays. | I also like the approach of storing strings | together with its length, but is is very un-C. Yeah, I have a little "genstring" (generic string) I use for things like this myself in programs of significant size. It keeps up with allocating itself. Standard library support for something like this would surely reduce the number of buffer-overflow security errors. Oh, well. | > 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. Really? I never knew that. Still, if the question is about how to engineer for portability, surely C99 isn't the best advice. | | 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