Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is it correct about "Destroying a member of built-in or compound typehas no effect"

Reply
Thread Tools

Is it correct about "Destroying a member of built-in or compound typehas no effect"

 
 
fl
Guest
Posts: n/a
 
      01-27-2013
Hi,
I read "C++ primer". In the part "The synthesized Destructor", it has the statements below the dot line. I especially do not understand why it mentions built-in type in its first line? The synthesized destructor only destroysthe user contructed class type?

Before the dot line citations, it has said that the synthesized destructor destroying the members in reverse order from the declaration. What can the synthesized destructor do? I am confused after the author mentions built-inand compound type. Could you explain it to me?

Thanks,


......
"Destroying a member of built-in or compound type has no effect. In particular, the synthesized destructor does not delete the object pointed to by a pointer member"
 
Reply With Quote
 
 
 
 
fl
Guest
Posts: n/a
 
      01-27-2013
On Sunday, January 27, 2013 1:17:22 PM UTC-5, Richard Damon wrote:
> On 1/27/13 10:11 AM, fl wrote:
>
> > Hi, I read "C++ primer". In the part "The synthesized Destructor", it

>
> > has the statements below the dot line. I especially do not understand

>
> > why it mentions built-in type in its first line? The synthesized

>
> > destructor only destroys the user contructed class type?

>
> >

>
> > Before the dot line citations, it has said that the synthesized

>
> > destructor destroying the members in reverse order from the

>
> > declaration. What can the synthesized destructor do? I am confused

>
> > after the author mentions built-in and compound type. Could you

>
> > explain it to me?

>
> >

>
> > Thanks,

>
> >

>
> >

>
> > ..... "Destroying a member of built-in or compound type has no

>
> > effect. In particular, the synthesized destructor does not delete the

>
> > object pointed to by a pointer member"

>
> >

>
>
>
> What it is pointing out is that the effective destructor for built-in
>
> types is a nop. There is no action needed to destroy them. It is also
>
> pointing out a particular danger that might be over looked in that for a
>
> pointer, its destruction does NOT free the memory it might be pointing
>
> to (largely because for a plain pointer, the compiler can't know that
>
> deleting the pointed object is the right action, there may well be other
>
> pointers to that object, or the object may not have been created with new).
>
>
>
> Some people might expect that deleting an integer, for example, might
>
> set it to zero, or deleting a pointer might delete the object pointed
>
> to, and the book is being clear about that.
>
>
>
> For members that DO have destructors (explicitly defined or synthesized)
>
> that destructor is called. In particular, if a member somewhere down the
>
> line is explicitly defined, that defined destructor will get called, and
>
> it will do what is needed to clean up that sub-object.


For integer in a class, if the destructor does not care about it, is there a memory leakage? Thanks,
 
Reply With Quote
 
 
 
 
goran.pusic@gmail.com
Guest
Posts: n/a
 
      01-28-2013
On Sunday, January 27, 2013 8:12:29 PM UTC+1, fl wrote:
> On Sunday, January 27, 2013 1:17:22 PM UTC-5, Richard Damon wrote:
>
> > On 1/27/13 10:11 AM, fl wrote:

>
> >

>
> > > Hi, I read "C++ primer". In the part "The synthesized Destructor", it

>
> >

>
> > > has the statements below the dot line. I especially do not understand

>
> >

>
> > > why it mentions built-in type in its first line? The synthesized

>
> >

>
> > > destructor only destroys the user contructed class type?

>
> >

>
> > >

>
> >

>
> > > Before the dot line citations, it has said that the synthesized

>
> >

>
> > > destructor destroying the members in reverse order from the

>
> >

>
> > > declaration. What can the synthesized destructor do? I am confused

>
> >

>
> > > after the author mentions built-in and compound type. Could you

>
> >

>
> > > explain it to me?

>
> >

>
> > >

>
> >

>
> > > Thanks,

>
> >

>
> > >

>
> >

>
> > >

>
> >

>
> > > ..... "Destroying a member of built-in or compound type has no

>
> >

>
> > > effect. In particular, the synthesized destructor does not delete the

>
> >

>
> > > object pointed to by a pointer member"

>
> >

>
> > >

>
> >

>
> >

>
> >

>
> > What it is pointing out is that the effective destructor for built-in

>
> >

>
> > types is a nop. There is no action needed to destroy them. It is also

>
> >

>
> > pointing out a particular danger that might be over looked in that for a

>
> >

>
> > pointer, its destruction does NOT free the memory it might be pointing

>
> >

>
> > to (largely because for a plain pointer, the compiler can't know that

>
> >

>
> > deleting the pointed object is the right action, there may well be other

>
> >

>
> > pointers to that object, or the object may not have been created with new).

>
> >

>
> >

>
> >

>
> > Some people might expect that deleting an integer, for example, might

>
> >

>
> > set it to zero, or deleting a pointer might delete the object pointed

>
> >

>
> > to, and the book is being clear about that.

>
> >

>
> >

>
> >

>
> > For members that DO have destructors (explicitly defined or synthesized)

>
> >

>
> > that destructor is called. In particular, if a member somewhere down the

>
> >

>
> > line is explicitly defined, that defined destructor will get called, and

>
> >

>
> > it will do what is needed to clean up that sub-object.

>
>
>
> For integer in a class, if the destructor does not care about it, is there a memory leakage? Thanks,


That's a strange question!

You can leak memory EXCLUSIVELY with pointers to dynamic memory (aka heap).

For each pointer value[1] you got from "new", there should be exactly one "delete". If you do that, there's no memory leaks.

A plain int has nothing to do with heap.

[1] Word "value" is important here:

int* p = new int(3);
int* p2 = p;
delete p; // OK, great.
delete p2; // BUG! It is illegal to delete same value twice!

HTH,

Goran.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the correct grammar to make a function call by using static member data which is a pointer to a ordinary class member function? zaeminkr@gmail.com C++ 3 07-06-2007 12:50 PM
Correct White Balance Doesn't Mean Correct Color?? jim evans Digital Photography 28 12-27-2005 05:10 AM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
correct or not correct? Dan HTML 7 10-02-2003 10:16 PM
To correct my program. please, check to find errors and correct me. joon Java 1 07-08-2003 06:13 AM



Advertisments