# Macro crashes after defining a large matrix

**URL:** <https://root-forum.cern.ch/t/macro-crashes-after-defining-a-large-matrix/40317>\
**Category:** Newbie\
**Created:** [July 8, 2020, 3:52pm UTC](https://root-forum.cern.ch/t/macro-crashes-after-defining-a-large-matrix/40317 "2020-07-08T15:52:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nyx](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Nyx](https://root-forum.cern.ch/u/Nyx)\
**Post date:** [July 8, 2020, 3:52pm UTC](https://root-forum.cern.ch/t/macro-crashes-after-defining-a-large-matrix/40317/1 "2020-07-08T15:52:51Z")

</div>

I have a Macro that works fine up to the line where I define a matrix in the form `float V[19999][19999]`.  
The only thing I do to run my macro is `root -l macro.cpp`. This shows a few debugging cout I have, and stops at the definition of the matrix. It exits ROOT with no error message.

This is a code that was given to me using arrays (for smaller dimension), so I would really like to continue using this arrays instead of vectors

---

<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:** [July 8, 2020, 4:06pm UTC](https://root-forum.cern.ch/t/macro-crashes-after-defining-a-large-matrix/40317/2 "2020-07-08T16:06:23Z")

</div>

You try to allocate 19999_19999_4 = 1,599,840,004 bytes on the stack. You cannot. If you don’t want to use vectors, try to allocate the matrix on the heap:

```auto
float **V = new float*[19999];
for (int i = 0; i < 19999; ++i)
    V[i] = new float[19999];

```

and then, don’t forget to delete it once you’re done:

```auto
for (int i = 0; i < 19999; ++i)
    delete [] V[i];
delete [] V;

```

If you don’t know the difference between the stack and the heap, you can read [this](https://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/) (for example)

---

<div class="post-metadata">

**Author:** ![Nyx](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Nyx](https://root-forum.cern.ch/u/Nyx)\
**Post date:** [July 8, 2020, 8:12pm UTC](https://root-forum.cern.ch/t/macro-crashes-after-defining-a-large-matrix/40317/3 "2020-07-08T20:12:41Z")

</div>

Thank you so much! I didn’t know the difference and I learnt something new. Just a good practice question: If my variable is used during the entire macro, should I still have to use **`delete`** to return the memory?

---

<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:** [July 8, 2020, 8:17pm UTC](https://root-forum.cern.ch/t/macro-crashes-after-defining-a-large-matrix/40317/4 "2020-07-08T20:17:00Z")

</div>

Yes, when you allocate memory, it’s a good practice to free it at the end, or whenever you don’t need it anymore
