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]

alloca in C++ programs [Was: Should this work?]


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)


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