Re: [ROOT] using "new" operator in loops

From: Victor Perevoztchikov (perev@bnl.gov)
Date: Thu Oct 11 2001 - 22:03:17 MEST


Hi Bill,

> Should the TObjArray be deleted at the end of the loop? Or should I
> not use the "new" operator to create it?

the second is correct. But content of TObjArray will not be deleted.
Victor

William Hanlon wrote:
> 
> Hi,
> 
> This is more of a C++ question I suppose. In a loop how should objects be
> created? Since C++ allows variable initialization anywhere to allow
> limited variable scope, does the same apply to objects created using
> the "new" operator?
> 
> For example I do something like the following in one of my programs:
> 
> while(...)
> {
> 
>       // Fill histograms with data
>          .
>          .
>          .
> 
>       // TObjArray will hold histograms sorted by scattering angle.
>       TObjArray *mlist = new TObjArray(nhit);
>       TIter next(mlist);
>       for (Int_t i = 0; i < nhit; i++)
>         mlist->Add(mir[sindex[i]]);
> 
>       // prepare the canvas for drawing
>       c1->Clear();
>       c1->Divide(nhit, 0);
>       c1->Draw();
> 
>       TH1F *temp;
>       Int_t count = 0;
>       while (temp = (TH1F*)next())
>         {
>           c1->cd(++count);
>           temp->Draw();
>         }
>       c1->Update();
> 
>       // continue looping
> 
> }
> 
> Should the TObjArray be deleted at the end of the loop? Or should I
> not use the "new" operator to create it?
> 
> Thanks,
> Bill

-- 
Victor M. Perevoztchikov   perev@bnl.gov  perev@vxcern.cern.ch       
Brookhaven National Laboratory MS 510A PO Box 5000 Upton NY 11973-5000
tel office : 631-344-7894; fax 631-344-4206; home 631-345-2690



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