TTree memory allocation issues with 2D array and std::vector

Hi everyone,
I encounter a strange problem with a TTree containing a 2D array of doubles and a std::vector.
First my general setup: All my code is fully compiled with gcc and runs standalone linked vs. ROOT. The code to loop over the TTree I am currently looking was generated using TTree::MakeClass().

The tree I use has dozens of branches, of which two give me problems:

Float_t         ahc_hitPos[7289][3];   //[ahc_nHits]
vector<double>  *trkLars_CosPhi;

I disable all branches except for these two (and the ahc_nHits branch to know how far I can loop in ahc_hitPos)


When I have both ahc_hitPos and trkLars_CosPhi branches enabled, access to ahc_hitPos works fine, while interacting with trkLars_CosPhi (e.g. trkLars_CosPhi->size()) causes an immediate segfault.
In this case the memory addresses of ahc_hitPos and trkLars_CosPhi are far away from each other:

ahc_hitPos: 0x17911a14  
trkLars_CosPhi: 0xc2d2000044c2b87b

When I disable only ahc_hitPos, trkLars_CosPhi works fine though. Intriguingly in this case, the memory address of trkLars_CosPhi is “close” to the other variables in the same program:

ahc_hitPos: 0x8d83a14 (empty array in this case)
trkLars_CosPhi: 0x8bedb90

It seems to me that in the first case (both branches enabled) the vector pointer points into memory-nirvana.

Does anybody have any idea how to fix this problem? Like this i cannot have ahc_hitPos and trkLars_CosPhi active at the same time.

Thanks in advance and cheers,

Your “ahc_hitPos[7289][3]” is a 2D array, but “//[ahc_nHits]” denotes a simple 1D variable length array.
Also, make sure that before you call “SetBranchAddress”, you initialize: vector<double> *trkLars_CosPhi = 0; or vector<double> *trkLars_CosPhi = new vector<double>;

That is true, however none of the behaviour changes if I change that line to


which in my understanding rules out buffer overflow problems (e.g. the hitPos writing beyond its assigned memory into trkLars_CosPhi territory, which is additionally not even near the same memory address.).

I looked up the code that is generating the TTree and there it is defined as a 2d histo though

hostTree->Branch(string(_prefix+"hitPos").c_str(),   &_hitsFill.hitPos, 

Any idea how what could check more in this direction?

edit: Is it possible that TTree::MakeClass() does not generate proper code for 2d arrays? Even if that was the case, I currently don’t see where i would have to change my code. :S

edit2: The vector pointers are already initialized of course!

I’m sorry if I reiterate questions that have probably been answered plenty of ties before.
Concerning the 1D variable length array streamer interaction described in the link, how would that effect my externally compiled software?

I found and fixed my problem, everything works now. Thank you for your input!

As usual with the most mysterious problems, the solution was rather simple and entirely my fault in implementation.
In the end it boiled down to confusions in my namespace, which lead to the vectors notbeing initialized before the first TTree::GetEntry().

Just make sure that you initialize all pointers before the corresponding “SetBranchAddress” are called. See TBranchElement::SetAddress(void* addobj) for more explanations.