Index of given array of histogram exceeds size of array

lvb_qqqq_Profile.C (865 Bytes)
lvb_qqqq_new.h (36.4 KB)
Hi Experts,
I am trying to make an array of 1-D histograms. On declaring them globally & defining them using a function, I found that I can fill the given array of histogram for index N exceeding the defined size of array without any error. I have attached my header & .C file .
For example, I declared
TH1F h3[3] ;
TH1F h4[3] ;
& defined them in another function
So I could easily done
h3[4].Fill(2.0);
Without any error. It is supposed to give error as the only upto index 2 for h3 defined.
I have attached my code so you could run it by yourself & see the problem. Let me know if anyone know the possible reason for this bizarre behaviour.
Arjun

P.S: In case you need it , commands to run
g++ -Wno-deprecated lvb_qqqq_Profile.C -o test.exe -I$ROOTSYS/include -L$ROOTSYS/lib root-config --cflags root-config --libs -std=c++11
./test.exe

It is YOUR (the programmer’s) responsibility to ensure that all indices are correct.

Thanks for the reply. As you see in my code, I am ensuring that indices are correct. But irrespective of it , problem arises. So my question is that how is this possible ?
Arjun

I fail to see any single place where you check indices.
For example, you should use something like this:

TH1F *h_mcobject_pt[7];
// ...
for(Int_t k = 0; k < ((Int_t)(sizeof(h_mcobject_pt)/sizeof(TH1*))); k++) { /*...*/ }

So, I followed your idea and add the snippet.

TH1F h_mcobject_pt[7];
// …
Int_t size = (sizeof(h_mcobject_pt)/sizeof(TH1
*)) ;
cout <<"Size = " << size ;
for(Int_t k = 0; k < size; k++) { // }

Output :-
Size = 7
, h_mcobject_pt[0].name() : Pt(topb)
, h_mcobject_pt[1].name() : Pt(topWl)
, h_mcobject_pt[2].name() : Pt(higgWq1)
, h_mcobject_pt[3].name() : Pt(higgWq2)
, h_mcobject_pt[4].name() : Pt(higgWl)
, h_mcobject_pt[5].name() : Pt(assob)
, h_mcobject_pt[6].name() : Pt(forwq)
, h_mcobject_pt[7].name() : Eta(topb)
, h_mcobject_pt[8].name() : Eta(topWl)
, h_mcobject_pt[9].name() : Eta(higgWq1)
, h_mcobject_pt[10].name() : Eta(higgWq2)
, h_mcobject_pt[11].name() : Eta(higgWl)
, h_mcobject_pt[12].name() : Eta(assob)
, h_mcobject_pt[13].name() : Eta(forwq)

So, irrespective of that, situation is same. Any more thoughts?
Arjun

The { /*...*/ } block in my previous post was not supposed to be dummy. It’s where you should put all your statements, e.g.: { std::cout << h_mcobject_pt[k]->GetName() << std::endl; }

I get your idea & I have done that earlier. It gives me output accordingly.

, h_mcobject_pt[0].name() : Pt(topb)
, h_mcobject_pt[1].name() : Pt(topWl)
, h_mcobject_pt[2].name() : Pt(higgWq1)
, h_mcobject_pt[3].name() : Pt(higgWq2)
, h_mcobject_pt[4].name() : Pt(higgWl)
, h_mcobject_pt[5].name() : Pt(assob)
, h_mcobject_pt[6].name() : Pt(forwq)

But the main problem is, when I put output statement out of the loop for k > 7, I still get the output whereas it is supposed to give error.This is my problem.

Arjun

std::array::at
std::vector::at

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.