Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 13 Jul 2002 15:13:30 -0400
From:      Brian T.Schellenberger <bts@babbleon.org>
To:        Ingo Oeser <ingo.oeser@informatik.tu-chemnitz.de>
Cc:        Yar Tikhiy <yar@comp.chem.msu.su>, hackers@FreeBSD.ORG
Subject:   Re: Large variables on stack
Message-ID:  <20020713191331.540ECBA05@sakura.fake.com>
In-Reply-To: <20020713174755.E758@nightmaster.csn.tu-chemnitz.de>
References:  <20020712194809.A62768@comp.chem.msu.su> <20020712155951.B4F0BBA05@i8k.babbleon.org> <20020713174755.E758@nightmaster.csn.tu-chemnitz.de>

next in thread | previous in thread | raw e-mail | index | archive | help
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 <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

-- 
Brian, the man from Babble-On . . . .   bts@babbleon.org (personal)
                                        http://www.babbleon.org

http://www.eff.org                      http://www.programming-freedom.org 

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?20020713191331.540ECBA05>