Deleteing automatic variables

B

Blue Ocean

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.
 
P

Phlip

Blue Ocean said:
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.
 
R

Rolf Magnus

Blue said:
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".
 
H

Howard

Rolf Magnus said:
Blue said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top