# Dynamically allocated array

**URL:** <https://root-forum.cern.ch/t/dynamically-allocated-array/41997>\
**Category:** Newbie\
**Created:** [October 25, 2020, 7:47pm UTC](https://root-forum.cern.ch/t/dynamically-allocated-array/41997 "2020-10-25T19:47:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![boboc](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/c2a13f/32.png) [@boboc](https://root-forum.cern.ch/u/boboc)\
**Post date:** [October 25, 2020, 7:47pm UTC](https://root-forum.cern.ch/t/dynamically-allocated-array/41997/1 "2020-10-25T19:47:36Z")

</div>

Hello!

I am new to ROOT and OOP, in general.

I am trying to do a program that writes into a “.root” file a number of objects “Event” which containt a variable number of “Waveform” objects, “Waveform” having 1024 “x” coordinates and 1024 “y” coordinates.

When I use “task.C” program in which Waveform is statically allocated the program works well; when I open TBrowser and navigate to “output.root” -\> waveform -\> w[2] -\> (let’s say) x, I see 4096 entries, but in the case of “task\_dyn.C” I only see 2048 entries (nevent \* 2).

My question is: am I doing right the dynamically allocation in the case of “task\_dyn.c”? (because I think that’s the problem). I attached the two “.C” files I’m reffering.

Thanks in advance!

* * *

[task\_dyn.C](https://root-forum.cern.ch/uploads/short-url/9P4AvVodcfVp7zkb8ii5BCTVElR.C) (1.2 KB) [task.C](https://root-forum.cern.ch/uploads/short-url/hnAmfjLgieT8et7WpfWnICFGCMm.C) (1.1 KB)

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [October 26, 2020, 8:25am UTC](https://root-forum.cern.ch/t/dynamically-allocated-array/41997/2 "2020-10-26T08:25:15Z")

</div>

Welcome to the ROOT forum!  
You should use:

```auto
   w = new Waveform[noWaveforms];

```

instead of:

```auto
   w = (Waveform*) malloc(noWaveforms * sizeof(Waveform));

```

- `malloc` allocates uninitialized memory.
- `new` initializes the allocated memory by calling the constructor. It does not need you to manually specify the size you need and cast it to the appropriate type.

---

<div class="post-metadata">

**Author:** ![boboc](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/c2a13f/32.png) [@boboc](https://root-forum.cern.ch/u/boboc)\
**Post date:** [October 26, 2020, 8:34am UTC](https://root-forum.cern.ch/t/dynamically-allocated-array/41997/3 "2020-10-26T08:34:26Z")

</div>

Thank you @bellenot, but, unfortunately, this does not solves the problem.

I still have 2048 entries instead of 4096 (for nevent = 2 and noWaveforms = 2). This is how ROOT Object Browser looks like.

 ![Screenshot_2020-10-26_10-30-06](https://root-forum.cern.ch/uploads/default/original/3X/5/f/5f667f15e2366a52be05228ac8289af6f98db036.png)

In the case of static allocation (Waveform w[2] instead of Waveform \* w), ROOT Object Browser looks like this (this is how it should be).

 ![Screenshot_2020-10-26_10-33-49](https://root-forum.cern.ch/uploads/default/original/3X/0/1/0150f99d4f0e5b719871cb3138377ec4af5697aa.png)

I still can’t figure out what is the problem.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [October 26, 2020, 8:54am UTC](https://root-forum.cern.ch/t/dynamically-allocated-array/41997/4 "2020-10-26T08:54:54Z")

</div>

> [@boboc](#):
>
> I still have 2048 entries instead of 4096 (for nevent = 2 and noWaveforms = 2). This is how ROOT Object Browser looks like.

I think there is something wrong in your code, since you only construct one `Event` class in both versions of your code… I’m trying to understand and I’ll let you know once I find what the issue is

---

<div class="post-metadata">

**Author:** ![boboc](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/c2a13f/32.png) [@boboc](https://root-forum.cern.ch/u/boboc)\
**Post date:** [October 26, 2020, 9:25am UTC](https://root-forum.cern.ch/t/dynamically-allocated-array/41997/5 "2020-10-26T09:25:25Z")

</div>

Hello, @bellenot!

I solved the problem using “std::vector”. Here is the source code.

[task\_dyn.C](https://root-forum.cern.ch/uploads/short-url/wKoNEKVEhHvY76Dj1a4C08OUwAW.C) (1.3 KB)

Thank you!

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [October 26, 2020, 9:28am UTC](https://root-forum.cern.ch/t/dynamically-allocated-array/41997/6 "2020-10-26T09:28:29Z")

</div>

OK, good! And you’re welcome
