Deleteing automatic variables

Discussion in 'C++' started by Blue Ocean, Jul 10, 2004.

  1. Blue Ocean

    Blue Ocean Guest

    Suppose I have a class. It has some members and some variables and
    all that. The methods are not important in this question.

    class IntSet
    {
    public:
    ...
    private:
    int threshold;
    int[] set;
    }

    Suppose I have a destructor and I want to make sure that all of the
    resources previously owned by this class will be given back to the
    system when an instance of the class is deleted. Should the
    destructor be:

    IntSet::~IntSet()
    {
    delete[] set;
    delete threshold;
    }

    Or should it be:

    IntSet::~IntSet()
    {
    delete[] set;
    }

    Or are both variants wrong?

    Thanks in advance for the help.
     
    Blue Ocean, Jul 10, 2004
    #1
    1. Advertising

  2. Blue Ocean

    Phlip Guest

    "Blue Ocean" <> wrote in message
    news:...
    > Suppose I have a class. It has some members and some variables and
    > all that. The methods are not important in this question.
    >
    > class IntSet
    > {
    > public:
    > ...
    > private:
    > int threshold;
    > int[] set;
    > }


    I don't see no pointers.

    int [] set won't compile, so you might use int * set, then write a
    constructor that calls set = new int [whatever];

    > Suppose I have a destructor and I want to make sure that all of the
    > resources previously owned by this class will be given back to the
    > system when an instance of the class is deleted. Should the
    > destructor be:
    >
    > IntSet::~IntSet()
    > {
    > delete[] set;
    > delete threshold;
    > }


    Noope. Only use 'delete' to free the product of 'new', and 'delete[]' to
    free the product of 'new[]'.

    If your members all have destructors that take care of themselves, you don't
    need to write a destructor.

    Learn how to use std::vector<int>. Read /Accelerated C++/, and learn the
    standard library stuff _first_. Don't be mislead by tutorials that start
    with the primitive crap; it causes trouble.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
     
    Phlip, Jul 10, 2004
    #2
    1. Advertising

  3. Blue Ocean

    Rolf Magnus Guest

    Blue Ocean wrote:

    > Suppose I have a class. It has some members and some variables and
    > all that. The methods are not important in this question.
    >
    > class IntSet
    > {
    > public:
    > ...
    > private:
    > int threshold;
    > int[] set;
    > }
    >
    > Suppose I have a destructor and I want to make sure that all of the
    > resources previously owned by this class will be given back to the
    > system when an instance of the class is deleted. Should the
    > destructor be:
    >
    > IntSet::~IntSet()
    > {
    > delete[] set;
    > delete threshold;
    > }
    >
    > Or should it be:
    >
    > IntSet::~IntSet()
    > {
    > delete[] set;
    > }
    >
    > Or are both variants wrong?


    Yes. Or rather, one is incomplete. The latter one is correct, but
    missing an assignment operator and copy constructor. You have to obey
    the "Rule of Three", which says: If you need one of destructor, copy
    constructor and assignment operator, you probably need all three of
    them".
     
    Rolf Magnus, Jul 10, 2004
    #3
  4. Blue Ocean

    Howard Guest

    "Rolf Magnus" <> wrote in message
    news:ccpnb4$s4k$01$-online.com...
    > Blue Ocean wrote:
    >
    > > Suppose I have a class. It has some members and some variables and
    > > all that. The methods are not important in this question.
    > >
    > > class IntSet
    > > {
    > > public:
    > > ...
    > > private:
    > > int threshold;
    > > int[] set;
    > > }
    > >
    > > Suppose I have a destructor and I want to make sure that all of the
    > > resources previously owned by this class will be given back to the
    > > system when an instance of the class is deleted. Should the
    > > destructor be:
    > >
    > > IntSet::~IntSet()
    > > {
    > > delete[] set;
    > > delete threshold;
    > > }
    > >
    > > Or should it be:
    > >
    > > IntSet::~IntSet()
    > > {
    > > delete[] set;
    > > }
    > >
    > > Or are both variants wrong?

    >
    > Yes. Or rather, one is incomplete. The latter one is correct,


    No, it's not, because set is not a pointer allocated by using new[]. In
    fact, the declaration of set is not legal at all!

    -Howard
     
    Howard, Jul 12, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Emilio

    Problem deleteing files

    Emilio, Oct 23, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    372
    Rajesh.V
    Oct 23, 2003
  2. Replies:
    1
    Views:
    407
    Thomas Matthews
    Jul 12, 2004
  3. Replies:
    5
    Views:
    504
    John Carson
    Mar 14, 2005
  4. msnnews.msn.com
    Replies:
    10
    Views:
    551
    gerry
    Aug 22, 2006
  5. timmy
    Replies:
    3
    Views:
    258
    Mikael Olofsson
    Nov 14, 2006
Loading...

Share This Page