473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Deleteing automatic variables

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.
Jul 22 '05 #1
3 1169

"Blue Ocean" <ag***********@gmail.com> wrote in message
news:af*************************@posting.google.co m...
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/bi...UserInterfaces
Jul 22 '05 #2
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".

Jul 22 '05 #3

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:cc*************@news.t-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

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Anthony Martin | last post by:
I've been reading the Java Language Specification, and in Chapter 16 there's an interesting topic called Definite Assignment. http://tinyurl.com/3fqk8 I'm wondering about the idea of "Deferred...
6
by: main() | last post by:
I'm a newbie. These questions arose out of my curiosity, Please pardon me if this questions sound silly. 1. Why are the automatic variables are left uninitialized by default( as i understand...
6
by: Nick Keighley | last post by:
Hi, Is this code fundamentally broken? class B { } class D: public B {
58
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is...
4
by: acw | last post by:
I am trying to understand how IE handles automatic tag variables. I know that IE will create a global variable each time it finds a tag with its name or id attribute set. If you have more than...
25
by: sidd | last post by:
In the following code: int i = 5; ---it goes to .data segment int j; ---it goes to bss segment int main() { int c; int i = 5; ---stack
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.