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

Discussion in 'C++' started by fl, Jan 27, 2013.

  1. fl

    fl Guest

    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, Jan 27, 2013
    #1
    1. Advertising

  2. fl

    fl Guest

    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,
    fl, Jan 27, 2013
    #2
    1. Advertising

  3. fl

    Guest

    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.
    , Jan 28, 2013
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Hubberstey
    Replies:
    0
    Views:
    1,056
    Tim Hubberstey
    Jul 3, 2003
  2. joon
    Replies:
    1
    Views:
    511
    Roedy Green
    Jul 8, 2003
  3. Dan

    correct or not correct?

    Dan, Oct 2, 2003, in forum: HTML
    Replies:
    7
    Views:
    434
  4. J.Ram
    Replies:
    7
    Views:
    642
  5. froil
    Replies:
    12
    Views:
    304
    Gunnar Hjalmarsson
    Mar 2, 2006
Loading...

Share This Page