# Crash when reading vector from tree BUT ONLY when compiled

**URL:** <https://root-forum.cern.ch/t/crash-when-reading-vector-from-tree-but-only-when-compiled/9187>\
**Category:** ROOT\
**Created:** [November 2, 2009, 4:54pm UTC](https://root-forum.cern.ch/t/crash-when-reading-vector-from-tree-but-only-when-compiled/9187 "2009-11-02T16:54:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![loicus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/b9bd4f/32.png) [@loicus](https://root-forum.cern.ch/u/loicus)\
**Post date:** [November 2, 2009, 4:54pm UTC](https://root-forum.cern.ch/t/crash-when-reading-vector-from-tree-but-only-when-compiled/9187/1 "2009-11-02T16:54:13Z")

</div>

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;
     }

  }

```

}

---

<div class="post-metadata">

**Author:** ![loicus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/b9bd4f/32.png) [@loicus](https://root-forum.cern.ch/u/loicus)\
**Post date:** [November 2, 2009, 5:00pm UTC](https://root-forum.cern.ch/t/crash-when-reading-vector-from-tree-but-only-when-compiled/9187/2 "2009-11-02T17:00:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [November 3, 2009, 12:13pm UTC](https://root-forum.cern.ch/t/crash-when-reading-vector-from-tree-but-only-when-compiled/9187/3 "2009-11-03T12:13:07Z")

</div>

[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 use`std::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.

---

<div class="post-metadata">

**Author:** ![loicus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/b9bd4f/32.png) [@loicus](https://root-forum.cern.ch/u/loicus)\
**Post date:** [November 3, 2009, 1:00pm UTC](https://root-forum.cern.ch/t/crash-when-reading-vector-from-tree-but-only-when-compiled/9187/4 "2009-11-03T13:00:44Z")

</div>

thanks Philippe,  
It solved the problem…

Loic,
