This is the mail archive of the gcc-bugs@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]

signal 11 generating problem, was: Further observations regarding alloca on i586-pc-linux-gnu


Martin von Loewis wrote:
> 
> > a) allocate heap memory inside the function. Disadvantage: needs
> >    explicite release.
> 
> I'd say auto_ptrs are there for you, no? Somebody correct me, but it
> seems that
> 
> void f(char * = std::auto_ptr<char*>(new char[20]));
> 
> is valid C++, and gives you memory that will live slightly longer than
> the call to f, and will then be automatically deallocated.
> 
> You did not say how long you need the memory: If you need automatic
> deallocation when the caller of f returns, you can return an auto_ptr
> from f.
> 

I had thought about auto_ptr before, but the SGI STL in egcs-1.0.x
disables <memory>. Alloca seemed to be a much easier solution.

Returning an auto_ptr is not really an option. According to Stroustrup, 
"The C++ Programming Language" 3rd Edition page 368, auto_ptr does not 
contain a cast operator to its element type.

However, the following program does what I want:

#include <sgimemory>
#include <cstdlib>
#include <iostream>

char *
xx (const auto_ptr<char> &c = auto_ptr<char>(new char[20]))
{
    return strncpy (c.get (), "Hello World!", 20);
}

int
main ()
{
    cout << xx () << endl;
}

<sgimemory> is the <memory> include file provided by the current
SGI STL implementation (don't know the exact version number, but
it was current as of 1 week ago).

Now the reason this has been sent to egcs-bugs, too. My original version
of the above program looks like this:

#include <sgimemory>
#include <cstdlib>
#include <iostream>

template <int i = 20> char *
xx (const auto_ptr<char> &c = auto_ptr<char>(new char[i]))
{
    return strncpy (c.get (), "Hello World!", i);
}

int
main ()
{
    cout << xx () << endl;
}

This version crashes gcc version egcs-2.90.27 980315 (egcs-1.0.2
release)
with g++: Internal compiler error: program cc1plus got fatal signal 11

Can somebody test this with egcs-1.1, please?

-- 
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]