Susan Baker wrote:[color=blue]
>
> The "magic" I was referring to (which everyone seems to have missed -
> perhaps I did not make my self clear enough) was this: how can
> auto_pointer correctly free memory allocated for a complicated structure
> like client - which has several pointer members.[/color]
And the answer is: it can't
typedef struct {
void *clientdata ;
FUNC_PTR1 cb1 ;
FUNC_PTR2 cb2 ;
struct param_ pdata ;
int size ;
} client;
You are talking about having an auto_ptr (or some other smart pointer)
to a client object. Well, the only thing the smart pointer does, is
to keep track of when the client object itself should be destroyed.
It is the job of the client object to take care of its inner details.
It does this by eg. making all the pointers in the client struct smart
pointers by themselfs or to simply do the delete in the destructor.
The smart pointer cares about the client object as a whole. The client
object is repsonsible for the inner details of itself.
[color=blue][color=green][color=darkred]
> >>3). Will the responses for the previous two statements hold true for a
> >>more complicated structure like client above?[/color]
> >
> >
> > If you maintain the 'creator is deleter' rule many 'problems' disappear
> > in C++.
> >[/color]
>
> True - but then what is the point of the container?.[/color]
To have one object that is reponsible for all the data in it.
[color=blue]
> I *thought* the
> container was "managing" mem resources for me - I can just as quickly
> roll my own "container" using an array.[/color]
Yes. Then the array *is* your container. No problem with that.
But don't roll your own. Use what is already available.
[color=blue]
> What "exactly" makes it a "safe
> pointer".[/color]
A safe pointer, or what we call a smart pointer, *ensures* that the delete
is executed. Even if you, the programmer have long forgotten that something
was dynamically allocated. It does this delete in all cases by the shear fact
that the smart pointer object is itself destroyed in some way. Be it that the
smart pointer is a member of some structure and an object of that structure
is allocated as a local variable and goes out of scope. Or that this structure
object is dynamically allocated and gets deleted.
There is no magic in it. Everything can be done 'by hand' also. The only problem
is, that humans tend to make mistakes and forget to do things. Then it is handy
to have an object that does the cleanup all by itself without the programmer having
to do anything except just using that smart pointer instead of an ordinary pointer.
--
Karl Heinz Buchegger
kbuchegg@gascad.at