Free or not to free ?

Free or not to free ?

Post by Patrick Gui » Mon, 27 Nov 2000 04:00:00



Dear all
I have a class which contains a vector<>

class Foo
{
typedef vector<Species *> SpeciesList;
public:
  template<class T>
  void Insert(int nargs, char *args[], const char *speciesname);

Quote:}

template<class T>
void PIC::Insert(int nargs, char *args[], const char *speciesname)
{
  T* ptr = new T (nargs, args, speciesname);
  species.insert(species.end(), ptr);
  Nspecies++;

Quote:}

I wonder whether I should explicitly free them with the destructor
like

Foo::~Foo ()
{
  SpeciesList::iterator i=species.begin();
  SpeciesList::iterator last=species.end();
  for ( ; i != last; ++i) {
    delete *i;
  }

Quote:}

or I do not need to do it and it is done automatically?
More generally when should I and should not I explicitly destroy objects ?
Sincerely
Patrick
======================================================================
                                  Patrick Guio
                    Institute of Physics, University of Oslo
                      P.O. box 1048, Blindern, N-0316 Oslo
               Tel : (+47) 22 84 40 60 - Fax : (+47) 22 85 56 71

                          URL : http://www.fys.uio.no
 
 
 

Free or not to free ?

Post by John Harriso » Mon, 27 Nov 2000 04:00:00



Quote:

> Dear all
> I have a class which contains a vector<>

> class Foo
> {
> typedef vector<Species *> SpeciesList;
> public:
>   template<class T>
>   void Insert(int nargs, char *args[], const char *speciesname);
> }

> template<class T>
> void PIC::Insert(int nargs, char *args[], const char *speciesname)
> {
>   T* ptr = new T (nargs, args, speciesname);
>   species.insert(species.end(), ptr);
>   Nspecies++;
> }

> I wonder whether I should explicitly free them with the destructor
> like

> Foo::~Foo ()
> {
>   SpeciesList::iterator i=species.begin();
>   SpeciesList::iterator last=species.end();
>   for ( ; i != last; ++i) {
>     delete *i;
>   }
> }

> or I do not need to do it and it is done automatically?
> More generally when should I and should not I explicitly destroy objects ?

The rule is very simple, if you allocated it, you delete it. (Note that
deleting and destroying are not the same thing. Destroying always happens
automatically). So in the example above because you've allocated the
pointers, you should delete them.

The above rule does have exceptions, for instance the auto_ptr class, but in
general it is followed.

john

- Show quoted text -

> Sincerely
> Patrick
> ======================================================================
>   Patrick Guio
>     Institute of Physics, University of Oslo
>       P.O. box 1048, Blindern, N-0316 Oslo
>        Tel : (+47) 22 84 40 60 - Fax : (+47) 22 85 56 71

>   URL : http://www.fys.uio.no


 
 
 

Free or not to free ?

Post by s.t.g » Mon, 27 Nov 2000 04:00:00


The way to think of it is that vector is like an array. In a normal array you would have
to delete stuff explicitly, so it is the same with vector. It doesn't take over the
ownership of objects *that you point to*.