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]

egcs-g++: what about a new warning (for dummy:) ?


	Hi ! 

I ran into a ^@|^i@ problem with template contructor and template 
assignement operator. This led me think about a warning.

In order to localize the problem, please, look at the following exemple:

template<class T> struct foo
{
 T *active_, *store_;
 size_t sz_;

 foo() : active_(0), store_(0), sz_(0) { }
 foo(size_t sz) : store_(new T[sz]), sz_(sz) { active_ = store_; }
 ~foo( ) { delete [] store_; }
 foo(const foo& f) : active_(f.active), store_(0), sz_(f.sz_) { } //copy_1
 template<class T2> foo( const foo<T2>& f ) : store_(new T[f.sz_]), sz_(f.sz_)
 {
   active_ = store_;
   for (size_t i=0,i<sz_;i++) active_[i] = f.active_[i];
 } // copy_2
 template<class T2> foo& operator=(const foo<T2>& f)
 {
   for (size_t i=0,i<sz_;i++) active_[i] = f.active_[i];
 }
};

template<class T> void use_a_copy(const foo<T>& f)
{
 foo<T> f1 = f;
 f[4] = T(-1); // for example;
 // ...
}
 
In the above function the copy constructor (copy_1) is called for f1. In its 
absence a default copy constructor is generated by egcs-g++ (as written in 
the "C++ programming Language", 3rd ed., p348): in this case, at the end of the 
function, the local variable f1 will be destroyed and in the same time 
the memory to which f.store_ are referring. This will lead to a segfault 
when f will get deleted (happening for example in __libc_free :)

Also, what about a warning of the type:
"template<class T> struct foo<T>: warning, no copy constructor defined, default 
copy constructor will be generated."

Note that with egcs-g++, and the above definition of struct foo<T> a default 
assignement operator will be generated for the sequence:
//..
foo<T> f1;
f1 = g; // with g of type foo<T>.
//..

This also will lead to same problem...

That's my .000000000000000000000002cts. Sorry, I can't send a patch cause 
egcs is  really beyond my knowledge :\

Thanx for the job !)

Regards, Max


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