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

F

fl

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"
 
F

fl

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,
 
G

goran.pusic

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top