Re: [Fwd: [ROOT] Little hint about pointers]

From: Rene Brun (Rene.Brun@cern.ch)
Date: Tue Nov 20 2001 - 17:20:55 MET


Alberto,

It is important to read the chapter on Collection classes in the Users Guide.
See this chapter starting at page 285.

one typically uses a TObjArray if one wants to address randomly entries
in the array.

One uses a THashList is the problem is to store objects identified
by a key or name and if one wants to get a pointer to an object giving
its key or name.

Rene Brun

Alberto Pulvirenti wrote:
> 
> ...and another hint: what improvement could I gain using THashList
> instead of TObjArray?
> 
> -------- Original Message --------
> Subject: [ROOT] Little hint about pointers
> Date: Tue, 20 Nov 2001 13:11:29 +0100
> From: Alberto Pulvirenti <alberto.pulvirenti@CT.INFN.IT>
> Organization: University of Catania
> To: roottalk <roottalk@pcroot.cern.ch>
> 
> Dear all (but maybe it's Rene who knows the answer)
> 
> I've a little question about dynamic memory allocation. Follow this
> example to understand what the question is.
> 
> I have two classes, say 'C1' and 'C2', inheriting from TObject. In
> particular, in the C2 is defined a data-member which points to a C1 object:
> 
> class C2 : public TObject {
> 
>    .
>    .
>    .
> 
> public:
> 
>    .
>    .
>    .
> 
>    C1 *fPointer;
> 
>    .
>    .
>    .
> 
> };
> 
> In my work, I have created a TObjArray, say obj_arr, where I stored many
> objects of class C1. When I create a C2 object, I initialize its
> fPointer data-member to point to one of the elements of the 'obj_arr'
> collection:
> 
> C2 *c = new C2;
> c->fPointer = (C1*)obj_arr->At(k);
> 
> However, I need to delete the C2 object, without deleting the C1 object
> it points to. Then, what must I put in the C2 destructor?
> 
> If I use this one:
> 
> C2::~C2() {
>     delete fPointer;
> }
> 
> will it erase the memory, cancelling the C1 object even if I keep in RAM
> the TObjArray where it was initially stored?
> Or this will remove only the link, but, given that there is another
> class which points to that memory slot (obj_arr, in this case), the C1
> object will be kept and not deleted?
> 
> The question comes from the observation I did that, without using the
> destructor I presented you, it seems to come a large memory leak, so I
> thought it was necessary to use it...
> 
> If I was clear, could you give me a hint about this question?
> 
> Regards by Alberto



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:51:09 MET