Hi all
I totally agree with Christian that we should not link with lib New by
default - but it does require people
re-checking code etc. As indicated in the previous I had found at least 4
such cases and there are obviious more.
Please take Christian's advice.
Fv
------------------------------------------------------
Flemming Videbaek
Physics Department
Brookhaven National Laboratory
>
> In summary: If your class contains pointers to objects, make sure that
> the constructor initialises them properly. Assigning Zero to a pointer
> is also an initialisation:
>
> class BrFoo : BrBar
> private:
> BrGnus* fGnus; // An external reference
> BrGnat* fGnat; // Owned by this object
> public:
> BrFoo()
> fGnus = 0;
> fGnat = new BrGnat;
> }
> void SetGnus(BrGnus* gnus=0) { fGnus = gnus; }
> void Print(Option_t* option="")
> cout << "BrFoo::Print(" << option << ")" << endl;
> if (fGnus) fGnus->Print(option);
> if (fGnat) fGnat->Print(option);
> }
> ...
>
> If your class is the owner of a pointer to an object, then you must
> delete it in the destructor. Test before deleting a pointer that it
> points to something.
>
> ~BrFoo()
> // fGnus is an external reference
> if (fGnat) delete fGnat;
> }
>
> The ANSI/ISO C++ standard says [3.7.3.2 paragraph 3],
>
> ... the deallocation function [operator delete] shall deallocate the
> storage referenced by the pointer, rendering invalid [= 0] all
> pointers refering to any part of the _deallocated_ _storage_.
>
> However, experience has shown that not all C++ compilers are ANSI/ISO
> compliant (even though they claim to be), so care should be taken to
> not dereference pointers to deallocated storage. GCC is compliant,
> however.
>
> In general, one should always be carefull when dereferencing
> pointers. If you can _guaranty_ that a pointer is always initialised
> and allocated in your class and no one else has deallocated the
> storage, you can safely dereference it. However, if you at some point
> in the execution of your class _may_ deallocated the storage, or the
> storage can be deallocated outside of the class (as in the case of
> external references), one should always test wether the pointer
> actually points to something, using a conditional statement as in
> BrFoo::Print above.
>
This archive was generated by hypermail 2b29 : Tue Apr 17 2001 - 08:12:42 EDT