# Why segfault occurs when SetBranchAddress without initializing pointer to nullptr

**URL:** <https://root-forum.cern.ch/t/why-segfault-occurs-when-setbranchaddress-without-initializing-pointer-to-nullptr/62571>\
**Category:** Newbie\
**Tags:** root\
**Created:** [January 2, 2025, 5:52pm UTC](https://root-forum.cern.ch/t/why-segfault-occurs-when-setbranchaddress-without-initializing-pointer-to-nullptr/62571 "2025-01-02T17:52:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [January 2, 2025, 5:52pm UTC](https://root-forum.cern.ch/t/why-segfault-occurs-when-setbranchaddress-without-initializing-pointer-to-nullptr/62571/1 "2025-01-02T17:52:08Z")

</div>

Dear experts

I always got segfault if I declare something like `std::vector<TVector3> a;` and then `SetBranchAddress("a", &a);`, if I didn’t initialize the pointer to `nullptr` first. But I found in source code it will set the pointer to `nullptr` forcibly anyway, so why segfault occurs?  
I mean [here](https://root.cern.ch/doc/master/TTree_8cxx_source.html#l08375)

---

<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:** [January 2, 2025, 6:22pm UTC](https://root-forum.cern.ch/t/why-segfault-occurs-when-setbranchaddress-without-initializing-pointer-to-nullptr/62571/2 "2025-01-02T18:22:07Z")

</div>

Yes, you must initialize (all) pointers to nullptr or a valid value otherwise the behavior of the problem is usually undetermined. In the case of `SetBranchAddress` it can not know if the random value held by the pointer (if you do not explicitly assign it) is a valid address or not. It has no choice but to assume it is a valid address and when it is not it will lead to a crash.

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [January 3, 2025, 3:48am UTC](https://root-forum.cern.ch/t/why-segfault-occurs-when-setbranchaddress-without-initializing-pointer-to-nullptr/62571/3 "2025-01-03T03:48:55Z")

</div>

Thank you for your answer, I understand that I must do it in principle, but in the source code I mentioned:

```auto
Int_t TTree::SetBranchAddress(const char* bname, void* addr, TBranch** ptr)
 8377{
 8378 TBranch* branch = GetBranch(bname);
 8379 if (!branch) {
 8380 if (ptr) *ptr = nullptr;
 8381 Error("SetBranchAddress", "unknown branch -> %s", bname);
 8382 return kMissingBranch;
 8383 }
 8384 return SetBranchAddressImp(branch,addr,ptr);
 8385}

```

it will be set to `nullptr` anyway, isn’t it?

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [January 3, 2025, 6:51am UTC](https://root-forum.cern.ch/t/why-segfault-occurs-when-setbranchaddress-without-initializing-pointer-to-nullptr/62571/4 "2025-01-03T06:51:29Z")

</div>

Note that `ptr` is a _pointer to a pointer_. If there is no branch, the pointer that `ptr` points to will be set to `nullptr` in the code that you linked:

```auto
if (ptr) *ptr = nullptr;

```

You can see this because `ptr` is de-referenced like `*ptr`, which results in a segfault if `ptr` is not pointing to a valid memory location. If you set it to `nullptr` before, there is no de-referencing happening because the `if (ptr)` will be false. But if it’s not initialized, it contains a random number and de-referencing results in a segfault.

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [January 3, 2025, 7:21am UTC](https://root-forum.cern.ch/t/why-segfault-occurs-when-setbranchaddress-without-initializing-pointer-to-nullptr/62571/5 "2025-01-03T07:21:48Z")

</div>

Ah, I see. It’s a pointer to a pointer and I need to set the upstream pointer to `nullptr`. Thank you!
