tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] Buiding binutils 2.17 (needs dynamic arrays).


From: Rob Landley
Subject: Re: [Tinycc-devel] Buiding binutils 2.17 (needs dynamic arrays).
Date: Wed, 3 Oct 2007 18:22:51 -0500
User-agent: KMail/1.9.6

On Wednesday 03 October 2007 7:49:53 am Daniel Glöckner wrote:
> On Wed, Oct 03, 2007 at 01:59:47AM -0500, Rob Landley wrote:
> > It boils down to a funky call to alloca()...
>
> One thing to note is that memory for VLAs is freed when the block ends.
> GCC does this by saving the stack pointer before allocating the VLA and
> restoring it at the end of the block.
>
> This approach doesn't work if we mix VLAs with alloca.
>
> #include <stdlib.h>
>
> void f(char *,char *);
>
> void g(int n)
> {
>   while(n--) {
>     char p[n];
>     char *q=alloca(n*7);
>     f(p,q);
>   }
> }
>
> void h(int n)
> {
>   while(n--) {
>     char *q=alloca(n*7);
>     f(q,q);
>   }
> }
>
> In g GCC will free the alloca'ed memory at the end of each iteration while
> in h it will free the memory at the end of the function.

Does that strike anybody else as an enormous non-obvious side effect?

I thought alloca() memory died at the end of the current block anyway.  Here 
you're saying you can't _guarnatee_ it'll survive beyond the end of the 
current block, you might as well treat it as if it does...

> The GCC approach has the benefit that it won't force you to have 0,5TB of
> memory if one calls
>
> void e(int n) {
>   while(n--) {
>     char p[n];
>     f(p,p);
>   }
> }

If you need to use gcc, you know where to find it.  If you want to improve 
alloca(), patches welcome.

Otherwise:

void e(int n) {
  char p[n];
  while(n--) {
    f(p,p);
  }
}

Rob
-- 
"One of my most productive days was throwing away 1000 lines of code."
  - Ken Thompson.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]