This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
alloca in C++ programs [Was: Should this work?]
- To: Noel Yap <nyap at garban dot com>, egcs at cygnus dot com
- Subject: alloca in C++ programs [Was: Should this work?]
- From: Joerg Pommnitz <pommnitz at darmstadt dot gmd dot de>
- Date: Fri, 21 Aug 1998 10:27:59 +0200
- Organization: GMD-IPSI
- References: <35DB5033.BF4EDEB0@darmstadt.gmd.de> <35DC0C6A.3181@garban.com> <35DC0CF7.ABF3473B@darmstadt.gmd.de> <35DC1315.6027@garban.com> <35DC171F.81FD9C86@darmstadt.gmd.de> <35DC7EAD.27B3@garban.com>
Noel Yap wrote:
>
> IIRC, inlining is a request that the compiler need not heed. I think
> you and I disagree on when alloca is executed. From the looks of it,
> your original code should execute alloca during the initialization phase
> of xy(). It should then deallocate the memory just before it returns
> from the function.
>
> Noel
There is no such thing as the "initialization phase" of a function
(or method in this case). The default initialization for the argument
passed to xy is called before xy (when else) from main. Some further
investigation convinced me, that there is really something fishy going
on. Here is another test program:
#include <cstdlib>
#include <cstring>
#include <iostream>
template <int i>
void *xx (void *c = alloca (i))
{
return strncpy (static_cast<char *>(c), "Hello World!\n", i);
}
int
main ()
{
int i;
cout << xx<20> () - &i; cout << endl;
cout << xx<21> () - &i; cout << endl;
cout << xx<22> () - &i; cout << endl;
cout << xx<23> () - &i; cout << endl;
cout << xx<24> () - &i; cout << endl;
}
It runs fine and shows that the memory passed to xx is indeed
allocated on the stack. The same program dies with a SEGV if
you change the lines to
cout << xx<20> () - &i << endl;
This is VERY strange, because I never access the memory pointed
to by the return value of xx. There really seems to be something
wrong here.
--
Regards
Joerg
GMD-IPSI, Dolivostr. 15, Zimmer 120, D-64293 Darmstadt
+49-6151-869-786 (Phone), -818 (FAX)