473,513 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a deconstructor question

Dear All,

I am a little confused.

//Objects of this class are partially filled arrays of doubles
class PFArray
{
public:
...
~PFArray();
private:
double *a; //for an array of doubles
..
};

PFArray::~PFArray()
{
delete [] a;
}

The code "double *a" in the private section of class PFArray doesn't
NECESSARILY tell that a is a pointer to an array (it coulde be a pointer
to a double, right?). Why the deconstructor use " delete [] a "? If a
instead is pointer to a double, then "delete a" should be used?

Dec 30 '05 #1
11 2924

"Xiaoshen Li" <xl**@gmu.edu> wrote in message
news:dp***********@osf1.gmu.edu...
Dear All,

I am a little confused.

//Objects of this class are partially filled arrays of doubles
class PFArray
{
public:
...
~PFArray();
private:
double *a; //for an array of doubles
..
};

PFArray::~PFArray()
{
delete [] a;
}

The code "double *a" in the private section of class PFArray doesn't
NECESSARILY tell that a is a pointer to an array (it coulde be a pointer
to a double, right?).
It *is* a pointer to type double. A pointer to an array would look like
e.g:

double (*a)[size];
Why the deconstructor use " delete [] a "?
It should only use 'delete[]' if the value of the pointer
'a' was returned 'new[]' or is NULL (0).
Otherwise the behavior is undefined.
If a instead is pointer to a double, then "delete a" should be used?


Only if 'a's value was returned by 'new' (not '[]').

a = new double; // allocates a single type double object
delete a; // deallocate the single type double object
// delete[] a; // undefined behavior.

a = new double[5]; // allocates array of five type double objects
delete[] a; // deallocate the array of five doubles//
delete a; // undefined behavior.

double d;
a = &d; // 'a's value not from 'new' or 'new[]'
delete d; // undefined behavior
delete[] d; // undefined behavior

Which C++ book(s) are you reading?

-Mike
Dec 30 '05 #2
Thank you for the help. But I am not sure you are right.
I took C++ class three years ago. Since then, I didn't use it.
Yesterday, I saw the code in the class passed by the professor:

class Person
{
public:
..
~Person();
private:
char *name;
};

Person::~Person()
{
delete [] name;
}

Since name could be pointing a single char or a char array, why the
destructor uses "delete [] name"?

Thank you again.

Dec 30 '05 #3
Xiaoshen Li wrote:
Thank you for the help. But I am not sure you are right.
He is.
I took C++ class three years ago. Since then, I didn't use it.
Yesterday, I saw the code in the class passed by the professor:

class Person
{
public:
..
~Person();
private:
char *name;
};

Person::~Person()
{
delete [] name;
}

Since name could be pointing a single char or a char array, why the
destructor uses "delete [] name"?


The rule is -- if you allocate with new[], you deallocate
with delete[]. If you allocate with new, you deallocate with
delete.

Therefore the code by "the professor" is fine only if storage
for 'name' is allocated with new[] (which is most probably
the case).

HTH,
- J.
Dec 30 '05 #4

"Xiaoshen Li" <xl**@gmu.edu> wrote in message
news:dp***********@osf1.gmu.edu...
Thank you for the help. But I am not sure you are right.
Why not? What specifically did I state that you
believe is not correct.
I took C++ class three years ago. Since then, I didn't use it. Yesterday,
I saw the code in the class passed by the professor:

class Person
{
public:
..
What's the code you left out?
~Person();
private:
char *name;
};

Person::~Person()
{
delete [] name;
}

Since name could be pointing a single char or a char array, why the
destructor uses "delete [] name"?


Because it's assuming that 'name' was given a value by
'new[]' (not 'new', nor the address operator). If this
assumption proves false, the code is broken.

Anyway, you should not be using 'C-style' strings,
use a std::string object instead, then all the issues of
allocation/deallocation disappear (the std:: string objects
handle their own memory management for you automatically).

#include <string>

class Person
{
std::string name;
}; // no need for 'new' or 'delete'

-Mike
Dec 30 '05 #5
Thank you very much, Mike.

#include <string>
using namespace std;

class Person
{
publice:
...
private:
string name;
int age;
char gender;
}

With above class, is destructor needed or not? My guess is not, am I
correct?

Mike Wahler wrote:

Anyway, you should not be using 'C-style' strings,
use a std::string object instead, then all the issues of
allocation/deallocation disappear (the std:: string objects
handle their own memory management for you automatically).

#include <string>

class Person
{
std::string name;
}; // no need for 'new' or 'delete'

-Mike


Dec 30 '05 #6
Nope, no destructor needed there, as name will delete its own buffer.
But don't import std...use std::string!

Dec 30 '05 #7
Could you kindly elaborate why "don't import std, instead use
std::string"? Thank you very much.

Paul Henderson wrote:
Nope, no destructor needed there, as name will delete its own buffer.
But don't import std...use std::string!


Dec 30 '05 #8
Oh, just better practice in many people's opinion [though I might start
an argument if I say that too strongly]. Basically, there's no point in
*having* the std namespace if you're just going to include it at global
scope everywhere, and there's a danger of symbol-name conflicts between
your code and bits of std. But it doesn't really matter :-)

Dec 30 '05 #9

Paul Henderson wrote:

<suggesting avoiding using namspace std>
Oh, just better practice in many people's opinion [though I might start
an argument if I say that too strongly]. Basically, there's no point in
*having* the std namespace if you're just going to include it at global
scope everywhere, and there's a danger of symbol-name conflicts between
your code and bits of std. But it doesn't really matter :-)


Please quote some context in your message.

It is true that if you put a using directive or using declaration in
your own source file, it only affects you. As long as you understand
the issue, it's up to you whether you do it. However, the code that
sparked this discussion was a class definition, which could well reside
in a header file. If so, the argument for explicitly qualifying names
from the std namespace in preference to a using directive or
declaration should be made much more strongly.

If you put using namespace std or even using std::string in a header
file, *everyone* who includes your header file gets the namespace
pollution, whether they want it or not. This is very different from you
deciding to accept namespace pollution contained within your own source
file.

So it might be fair to say "But it doesn't really matter" if you are
talking about a source file, particularly in a toy program you are
writing for practice. But in a header file it really does matter. Don't
put using directives or using declarations in header files.

Gavin Deane

Dec 30 '05 #10
Xiaoshen Li wrote:
Dear All,

I am a little confused.

//Objects of this class are partially filled arrays of doubles
class PFArray
{
public:
...
~PFArray();
private:
double *a; //for an array of doubles
..
};

PFArray::~PFArray()
{
delete [] a;
}

Put raw arrays away.
Use std::vector for elements of type of double.

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Dec 30 '05 #11
Xiaoshen Li wrote:
Thank you for the help. But I am not sure you are right.
I took C++ class three years ago. Since then, I didn't use it.
Do you mean you didn't use delete[] ?
Look here:
http://www.softsurfer.com/Archive/al...poly_simplify()

Point* vt = new Point[n]; // vertex buffer
int* mk = new int[n] = {0}; // marker buffer
// ...
delete vt;
delete mk;

There are still big amount of old code with this buggy way of destroying
arrays: delete instead of delete[]
Since name could be pointing a single char or a char array, why the
destructor uses "delete [] name"?


http://www.parashift.com/c++-faq-lit...html#faq-16.13

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Dec 30 '05 #12

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

Similar topics

3
4997
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
2630
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
2354
by: steflhermitte | last post by:
Dear cpp-ians, I have a class: class mImage { public: //constructors / destructor mImage(unsigned int nrLayers); ~mImage();
3
3060
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
2
327
by: Doug Wiley | last post by:
Hello All For reference MyClass myClass = new MyClass() myClass.MyMethod() It appears that if you throw an application exception in MyMethod(), the myClass deconstructor will be bypassed....
10
3391
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
3683
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
4022
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
4696
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
0
7175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7120
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5697
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
4754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3247
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1609
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
466
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.