Connecting Tech Pros Worldwide Help | Site Map

a deconstructor question

 
LinkBack Thread Tools Search this Thread
  #1  
Old December 30th, 2005, 02:45 PM
Xiaoshen Li
Guest
 
Posts: n/a
Default 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?




  #2  
Old December 30th, 2005, 03:15 PM
Mike Wahler
Guest
 
Posts: n/a
Default Re: a deconstructor question


"Xiaoshen Li" <xli6@gmu.edu> wrote in message
news:dp3k73$eq2j$1@osf1.gmu.edu...[color=blue]
> 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?).[/color]

It *is* a pointer to type double. A pointer to an array would look like
e.g:

double (*a)[size];
[color=blue]
> Why the deconstructor use " delete [] a "?[/color]

It should only use 'delete[]' if the value of the pointer
'a' was returned 'new[]' or is NULL (0).
Otherwise the behavior is undefined.
[color=blue]
> If a instead is pointer to a double, then "delete a" should be used?[/color]

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


  #3  
Old December 30th, 2005, 03:45 PM
Xiaoshen Li
Guest
 
Posts: n/a
Default Re: a deconstructor question

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.

  #4  
Old December 30th, 2005, 03:55 PM
Jacek Dziedzic
Guest
 
Posts: n/a
Default Re: a deconstructor question

Xiaoshen Li wrote:[color=blue]
> Thank you for the help. But I am not sure you are right.[/color]

He is.
[color=blue]
> 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"?[/color]

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.
  #5  
Old December 30th, 2005, 03:55 PM
Mike Wahler
Guest
 
Posts: n/a
Default Re: a deconstructor question


"Xiaoshen Li" <xli6@gmu.edu> wrote in message
news:dp3ni9$17t5$1@osf1.gmu.edu...[color=blue]
> Thank you for the help. But I am not sure you are right.[/color]

Why not? What specifically did I state that you
believe is not correct.
[color=blue]
> 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:
> ..[/color]

What's the code you left out?
[color=blue]
> ~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"?[/color]

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


  #6  
Old December 30th, 2005, 04:35 PM
Xiaoshen Li
Guest
 
Posts: n/a
Default Re: a deconstructor question

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:
[color=blue]
>
> 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
>
>[/color]

  #7  
Old December 30th, 2005, 05:15 PM
Paul Henderson
Guest
 
Posts: n/a
Default Re: a deconstructor question

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

  #8  
Old December 30th, 2005, 05:25 PM
Xiaoshen Li
Guest
 
Posts: n/a
Default Re: a deconstructor question

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

Paul Henderson wrote:[color=blue]
> Nope, no destructor needed there, as name will delete its own buffer.
> But don't import std...use std::string!
>[/color]

  #9  
Old December 30th, 2005, 05:25 PM
Paul Henderson
Guest
 
Posts: n/a
Default Re: a deconstructor question

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 :-)

  #10  
Old December 30th, 2005, 06:45 PM
Gavin Deane
Guest
 
Posts: n/a
Default Re: a deconstructor question


Paul Henderson wrote:

<suggesting avoiding using namspace std>
[color=blue]
> 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 :-)[/color]

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

  #11  
Old December 30th, 2005, 08:25 PM
Mateusz Łoskot
Guest
 
Posts: n/a
Default Re: a deconstructor question

Xiaoshen Li wrote:[color=blue]
> 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;
> }[/color]


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

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
  #12  
Old December 30th, 2005, 08:25 PM
Mateusz Łoskot
Guest
 
Posts: n/a
Default Re: a deconstructor question

Xiaoshen Li wrote:[color=blue]
> 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.[/color]

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[]
[color=blue]
> Since name could be pointing a single char or a char array, why the
> destructor uses "delete [] name"?[/color]

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

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.