# Correct way to initialize array of histograms?

**URL:** <https://root-forum.cern.ch/t/correct-way-to-initialize-array-of-histograms/41493>\
**Category:** Newbie\
**Created:** [September 17, 2020, 2:13pm UTC](https://root-forum.cern.ch/t/correct-way-to-initialize-array-of-histograms/41493 "2020-09-17T14:13:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MartinSerfling](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@MartinSerfling](https://root-forum.cern.ch/u/MartinSerfling)\
**Post date:** [September 17, 2020, 2:13pm UTC](https://root-forum.cern.ch/t/correct-way-to-initialize-array-of-histograms/41493/1 "2020-09-17T14:13:41Z")

</div>

Hi Rooters,

I was trying to send an array of histograms to a class and realized through the highlighting of my editor that I might be using a weird or unnecessary way of declaring my arrays of histograms. So far I used (because I found it somewhere on Google)

```auto
TH1F** h = new TH1F*[int];
...
delete[] h;

```

This worked so far but I never really understood the right syntax of delete for different scenarios, meaning when to use ` delete[] h` or `delete h` or even something like `delete() h`. So it would be great if you could explain what the real difference is and when to use which method or send me a link to this topic. 🙂

The other thing that bothers me: What is the difference to

```auto
TH1F* h[int];

```

? Is one better than the other or is there a deeper concept beneath that I simply didn"t grasp?

Your help is much appreciated! 🙂

_ROOT Version:_ 6.22/02  
_Platform:_ Centos7

* * *

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [September 17, 2020, 2:51pm UTC](https://root-forum.cern.ch/t/correct-way-to-initialize-array-of-histograms/41493/2 "2020-09-17T14:51:29Z")

</div>

Hi Martin,

For `delete` vs. `delete[]`: They need to match the `new` or `new[]` invocation. The version with brackets creates storage for arrays, the one without brackets creates a scalar object. The version with parentheses (`delete()`) I haven’t seen, I think.

The difference between the first approach with `new[]` and the second approach is mostly the lifetime of the array. The first approach is called “heap allocation”, and the created array stays alive until the `delete[]` call. In the second case (`TH1F* h[int]`), called “stack allocation”, the array will only live until the end of the current scope. At the end of the scope, it is automatically removed from the stack (no `delete[]` call). The histograms in the array, however, will in both cases stay alive until they are explicitly deleted.

I’d suggest to avoid dealing with raw arrays if possible. You can instead use an `std::vector`, e.g.

```auto
std::vector<TH1F *> histograms;
histograms.push_back(new THF1(...));
...

// You can access the std::vector like an array
histograms[0]->Fill(...);

// And you can pass it to a class that requires a pure array
FuncThatTakesHistArray(histograms.data());

```

Cheers,  
Jakob

---

<div class="post-metadata">

**Author:** ![MartinSerfling](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@MartinSerfling](https://root-forum.cern.ch/u/MartinSerfling)\
**Post date:** [September 18, 2020, 7:07am UTC](https://root-forum.cern.ch/t/correct-way-to-initialize-array-of-histograms/41493/3 "2020-09-18T07:07:14Z")

</div>

> [@jblomer](#):
>
> For `delete` vs. `delete[]` : They need to match the `new` or `new[]` invocation.

Thank you very much for this great explanation Jakob, I somehow never found this written down explicitly.  
The approach with vectors didn’t really cross my mind but it does look intriguing. Thank you for that suggestion! 🙂
