Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Deletion on a non pointer array in c++

When I have an array like this:

int* test = new int[50];
for (int i = 0; i < 50; i++)
{
    one[i] = dist4(rng);
}

(Filled with random numbers for testing)
I can free the memory like this

delete[] test;

But when I declare the array like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

int test[50];
for (int i = 0; i < 50; i++)
{
    test[i] = dist4(rng);
}

I can’t free the momory with delete or delete[].

So whats the proper way of freeing the memory here?

PS: the "dist4" function is just a random number generator:

random_device dev;
mt19937 rng(dev());
uniform_int_distribution<mt19937::result_type> dist4(1,4); // distribution in range [1, 4]

>Solution :

whats the proper way of freeing the memory here?

No need to free memory explicitly using delete or delete[]in the latter case.

Assuming int test[50]; is declared inside a function, it has automatic storage duration and when test goes out of scope it will be automatically destroyed.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading