Strange segfault

Here’s a basic piece of code:

{
  std::vector<TH1F*> e;

  for(int i=0;i<24;i++) { 
    e.push_back(new TH1F(Form("e%d",i),"",100,0.9,10)); 
    std::cout << e.at(i)->Integral() << std::endl; 
  }
  
}

When I run it, it gives me a seg fault. I am running root version 5.32/00 . Any clues as to what’s going on?

Update: checked under 5.34/01, same issue. What am I not getting?

[code]{
gSystem->Exec(“rm -f AutoDictvectorTH1F*”);
gInterpreter->GenerateDictionary(“vector<TH1F*>”, “TH1F.h;vector”);

std::vector<TH1F*> e;

for(int i = 0; i < 24; i++) {
TH1F *h = new TH1F(TString::Format(“e%d”,i), “”, 100, 0.9, 10);
e.push_back(h); // CINT limitation: no “new TH1F” allowed here
std::cout << e.at(i)->Integral() << std::endl;
}
}[/code]

Hi Willie,

so it looks like CINT cannot handle simultaneous construction of the TH1Fs and
vector::push_back. I tried another thing which was to create the vector with
the right size and just place the pointers at the correct index which also
worked.

Is there a pattern one should generally avoid here or is the answer just
"compile your code" since this should work?

Cheers,

b.


It looks like CINT  can't handle simultaneous new's and
push backs, as honk already mentioned.  And a software engineer colleague of mine is
telling me that [b]in general[/b] embedding a construction in a push back is known to be dangerous.

Anyway, here's the modification that worked.

[code]
{
  std::vector<TH1F*> e;

  for(int i=0;i<24;i++) { 
    TH1F* tmp =new TH1F(Form("e%d",i),"",100,0.9,10); 
    e.push_back(tmp);

    std::cout << e.at(i)->Integral() << std::endl; 

  }
}
[/code]

It looks like CINT can’t handle simultaneous new’s and
push backs, as honk already mentioned. And a software engineer colleague of mine is
telling me that in general embedding a construction in a push back is known to be dangerous.

Anyway, here’s the modification that worked.

{
  std::vector<TH1F*> e;

  for(int i=0;i<24;i++) { 
    TH1F* tmp =new TH1F(Form("e%d",i),"",100,0.9,10); 
    e.push_back(tmp);

    std::cout << e.at(i)->Integral() << std::endl; 

  }
}

I cannot see how that could that be dangerous (even in non-general cases). All
arguments to vector::push_back will be evaluated before it is called, so even
if new threw an exception the vector would still be in a consistent state. The
story might be different if one needs to work with a broken std library
implementation or a broken compiler, but that should hardly be the case for
std::vector (even less so in general).

My take would have been that variables should in general be declared in
the smallest possible scope – and allocating the histogram in the
vector::push_back does that.

So you think this is just a CINT deficiency?