Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to delete arrays explicitly in C++ for memory conservation?

In one function. I create a local arrray. char arr[20]; And before end of this function. Will compiler do garbage collection for me? Or I need to do delete by myself?

like image 862
Anders Lind Avatar asked May 10 '12 18:05

Anders Lind


People also ask

Do you need to delete array in C?

In C, if you are declaring your array as static ex: int a[10], no need to delete. It will be freed when your function ends or program terminates.

Can we delete static array?

No, you don't need to delete it because array has automatic storage duration. It will be released when goes out of each while loop. You need to call delete [] / new [] , and delete / new in pairs.

How do you free memory in an array?

If the array is declared statically, then we do not need to delete an array since it gets deleted by the end of the program/ block in which it was declared. If the array is declared dynamically, we need to free the memory allocated to it using the free() function.

How do I delete an array in C?

In C programming, an array is derived data that stores primitive data type values like int, char, float, etc. To delete a specific element from an array, a user must define the position from which the array's element should be removed. The deletion of the element does not affect the size of an array.


3 Answers

There is no Garbage Collection in C++.

However, if you use automatic variables, they will be destroyed when they fall out of scope.

As a rule, there should be 1 delete call for every new. If you have no new, you don't delete.

like image 66
John Dibling Avatar answered Oct 15 '22 23:10

John Dibling


You don't have to delete this array since you create it on stack. If you created the array using new then you would have to use delete to clean up.

like image 10
Łukasz Milewski Avatar answered Oct 15 '22 22:10

Łukasz Milewski


Local variables are destroyed at the end of the block (not necessarily function) in which they're created. For example:

void myfunc() { 
   int x[some_size];

   if (something) { 
       std::vector<std::string> y;
       // ...
   } // y will be destroyed here
   // more code
} // x will be destroyed here

If you want your array destroyed earlier than the exit from the function, you may want to make use of the same:

void f() { 
    // come code here   

    {
        int x[size];

         // code that uses x
    } // `x` gets destroyed here

    // more code
 }

I should add, however, that destroying the variable at that point may not affect memory usage. The memory isn't needed after you exit from the inner block, but it may not be released immediately either.

On the other hand, if you use something like std::vector instead of explicit dynamic allocation, destroying the object will (immediately) free the memory that was being used to store the object's data.

like image 2
Jerry Coffin Avatar answered Oct 15 '22 21:10

Jerry Coffin