[ROOT] using "new" operator in loops

From: William Hanlon (whanlon@physics.rutgers.edu)
Date: Thu Oct 11 2001 - 21:31:30 MEST


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



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