Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 13 Jul 2002 17:47:55 +0200
From:      Ingo Oeser <ingo.oeser@informatik.tu-chemnitz.de>
To:        "Brian T.Schellenberger" <bts@babbleon.org>
Cc:        Yar Tikhiy <yar@comp.chem.msu.su>, hackers@FreeBSD.ORG
Subject:   Re: Large variables on stack
Message-ID:  <20020713174755.E758@nightmaster.csn.tu-chemnitz.de>
In-Reply-To: <20020712155951.B4F0BBA05@i8k.babbleon.org>; from bts@babbleon.org on Fri, Jul 12, 2002 at 11:59:51AM -0400
References:  <20020712194809.A62768@comp.chem.msu.su> <20020712155951.B4F0BBA05@i8k.babbleon.org>

next in thread | previous in thread | raw e-mail | index | archive | help
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 <stdlib.h>
#include <stdio.h>
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<argc; i++) if (argv[i]) print_arg(argv[i]);
   return 0;
}

Regards

Ingo Oeser
-- 
Science is what we can tell a computer. Art is everything else. --- D.E.Knuth

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020713174755.E758>