Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Is it correct about "Destroying a member of built-in or compound typehas no effect" (http://www.velocityreviews.com/forums/t956989-is-it-correct-about-destroying-a-member-of-built-in-or-compound-typehas-no-effect.html)

fl 01-27-2013 03:11 PM

Is it correct about "Destroying a member of built-in or compound typehas no effect"
 
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"

fl 01-27-2013 07:12 PM

Re: Is it correct about "Destroying a member of built-in or compoundtype has no effect"
 
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,

goran.pusic@gmail.com 01-28-2013 02:03 PM

Re: Is it correct about "Destroying a member of built-in or compoundtype has no effect"
 
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.


All times are GMT. The time now is 09:33 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.