Re: writing/reading objects to/form a file

From: Axel Naumann <Axel.Naumann_at_cern.ch>
Date: Mon, 21 Nov 2005 11:29:15 +0100


Hi Chiara,

> But still I do not know why it crashes...
> Is it because of the initialization in the default constructor?

it's probably because you don't specify an array dimension for fPads. You should do

AliTOFCalParam *fPads[npad]; // assuming npad is a const int

so ROOT knows how many AliTOFCalParam objects to write. Attached files show that this works properly.
Axel.

#include "MyClass.h"
MyClass::MyClass() {

   for (int i=0; i<5; ++i)

      fArray[i]=0;
}

void MyClass::Init() {

   for (int i=0; i<5; ++i)

      fArray[i]=new TObject();
}

MyClass::~MyClass() {

   delete[] fArray;
}

#include "TObject.h"

class MyClass: public TObject {
 public:
   MyClass();
   ~MyClass();
   void Init();
   void PrintArray() {

      printf("fArray==%p\n",fArray);
   }
 private:
   TObject *fArray[5];
   ClassDef(MyClass,1);
};

void testwr(){

   MyClass* mc=new MyClass();
   TFile* f=new TFile("myclass.root","RECREATE");    mc->Write("mc");

   delete f;
   f=new TFile("myclass.root");
   mc=0;
   f->GetObject("mc",mc);
   mc->PrintArray();
   delete f;
} Received on Mon Nov 21 2005 - 11:29:10 MET

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:13 MET