This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: alloca + inline


dewar at gnat dot com (Robert Dewar) writes:

> > In fact, assuming almost anything where a reasonable implementation
> > could change, is not a good idea.  Certainly GCC will not apologize
> > for breaking code whose correctness depends on such assumptions.
> 
> Surely your comment does not apply to use of the attribute ...
> 
> For the interesting alloca case, I would certainly say that the
> attribute must be used. Whether it "works" with the attribute is an
> interesting discussion. There are various possible outcomes of such
> a discussion:
> 
> 1. It does not work now, and we don't care
> 
> 2. It works now, but by accident, and might stop working in the future
> 
> 3. It works now, and you can reasonably expect it to work in the future
> (obviously guarantees can be made in this environment).

Or

4. It works now, and that's a bug.  Please submit a patch to make it
   stop working...

In this case, the definition of 'alloca' is clear: the storage it
allocates lasts until the end of the function.  So, this program
should work (modulo typos):

static int get_int(void)
{
  char *s = alloca (1024);
  fgets (stdin, s);
  return atoi (s);
}

int main(void)
{
  int i;
  for (i = 0; i < 10000; i++)
        printf ("%d\n", get_int());
  return 0;
}

but it doesn't if you have less than 10Mb of stack space and get_int
gets inlined.

-- 
- Geoffrey Keating <geoffk at geoffk dot org>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]