Crash when reading vector from tree BUT ONLY when compiled

Hi all,

I have a very strange problem…
I am reading a TTree containing a std::vector (and other stuff).
So I am trying to read this with a code as simple as possible…

see the code below…

what is puzzling to me, is that it run without any problems if I run the code with CINT (“root -l Test.C”).
But when I compile the code before runing it crashes at the GetEntry line. (“root -l Test.C++”).

In advance, thanks for help
Loic,

//I SKIP THE INCLUDES
void Test(){
TChain* tree = new TChain(“shallowTree/tree”);
tree->Add(“tree.root”);

  std::vector<unsigned int>* charge;
  tree->SetBranchAddress("GainCalibrationcharge", &charge, NULL);

  std::cout << "TreeEntries = " << tree->GetEntries() << endl;
  for (unsigned int ientry = 0; ientry < tree->GetEntries(); ientry++) {
     tree->GetEntry(ientry);

     std::cout<<"vector size = "<<(*charge).size()<<endl;
     for(unsigned int i=0;i<(*charge).size();i++){
        std::cout<<"ChargeI = " << (*charge)[i] << endl;
     }

  }

}

Just an other quick question…
How may I define charge as a vector instead of a pointer on it?

std::vector charge;
instead of
std::vector* charge;

when I do not define it as a pointer, it complains in the SetBranchAddress
tree->SetBranchAddress(“GainCalibrationcharge”, &charge, NULL);

thanks again for help,
Loic

[quote]But when I compile the code before runing it crashes at the GetEntry line. (“root -l Test.C++”).
[/quote]In some case CINT and the compiler are behaving differently, in particular in the cases where the behavior is ‘undefined’ by the C++ standard; for example when variable are not initialized:[quote]std::vector* charge;
tree->SetBranchAddress(“GainCalibrationcharge”, &charge, NULL); [/quote]
So usestd::vector<unsigned int>* charge = 0; tree->SetBranchAddress("GainCalibrationcharge", &charge, NULL);

[quote]How may I define charge as a vector instead of a pointer on it?
[/quote]This is currently only supported via the cumbersome: std::vector<unsigned int> charge; std::vector<unsigned int> *charge_ptr = &charge;

Cheers,
Philippe.

thanks Philippe,
It solved the problem…

Loic,