How to free an array in C/C++

29,833

Solution 1

In this program

int main() {
    // Will this code cause memory leak?
    // Do I need to call the free operator?
    // Do I need to call delete?
    int arr[3] = {2, 2, 3};
    return 0;
}

array arr is a local variable of function main with the automatic storage duration. It will be destroyed after the function finishes its work.

The function itself allocated the array when it was called and it will be destroyed afetr exiting the function.

There is no memory leak.

You shall not call neither C function free nor the operator delete [].

If the program would look the following way

int main() {
    int *arr = new int[3] {2, 2, 3};
    //...
    delete [] arr;
    return 0;
}

then you should write operator delete [] as it is shown in the function.

Solution 2

You have to call free only for objects you have created with malloc, and delete for those you've created with new.

In this case you don't need to do anything, this variable is managed automatically. As soon as it goes out of scope (i.e., in this case at the end of main()) its memory will be freed automatically.

So:

  1. No, there are no memory leaks here.
  2. The question doesn't make much sense. All variables are in RAM. The stack is just a special part of the RAM memory, which is managed in a way that results fast and efficient to deal with function calls, automatic allocation/deallocation of local variables, etc. Anyway, yes, arr is created on the stack, whereas variables created using malloc or new are said to be on the "free store" or "heap".

Solution 3

The stack is in RAM.

No, it doesn't leak memory. Yes, the array will normally be "on the stack" (i.e., wherever the implementation allocates local variables, which is required to have stack-like behavior, even though the hardware might not provide direct support for a stack).

Share:
29,833
Dr. Programmer
Author by

Dr. Programmer

Updated on October 26, 2020

Comments

  • Dr. Programmer
    Dr. Programmer over 3 years
    int main() {
        // Will this code cause memory leak?
        // Do I need to call the free operator?
        // Do I need to call delete?
        int arr[3] = {2, 2, 3};
        return 0;
    }
    
    1. Does this code create a memory leak?

    2. Where does arr reside? On the stack or in RAM?

    • Ivan
      Ivan over 8 years
      C and C++ are two completely different languages. Which one are you using?
  • Dr. Programmer
    Dr. Programmer over 8 years
    Thank's for the clarification. If I were to initialize a const char * str = "abc" or char str [] = "abc", Would that reside on the stack as well?
  • cadaniluk
    cadaniluk over 8 years
    @Dr.Programmer String literals are not stored on the stack, except if used to initialize an array like char str[] = "abc";. So your first example isn't on the stack but probably in read-only memory together with the program code or some data, your second example is on the stack.
  • Dr. Programmer
    Dr. Programmer over 8 years
    Thank's for the clarification. If I were to initialize a const char * str = "abc" or char str [] = "abc", Would that reside on the stack as well?
  • Dr. Programmer
    Dr. Programmer over 8 years
    Thank's for the clarification. If I were to initialize a const char * str = "abc" or char str [] = "abc", Would that reside on the stack as well?
  • Jerry Coffin
    Jerry Coffin over 8 years
    Assuming the first was inside a function, the pointer would be allocated as a local, but the string literal would be statically allocated (e.g., usually in an initialized data segment). In the second, you'd have a string literal that was statically allocated, and an array that was locally allocated. The string literal would be used to initialize the local array upon entry to the function (or whatever block) in which it was located.
  • cadaniluk
    cadaniluk over 8 years
  • Dr. Programmer
    Dr. Programmer over 8 years
    How does the delete operator know the size of the array?
  • Bo Persson
    Bo Persson over 8 years
    @Dr.Programmer - Probably by magic. The language standard doesn't say how it works, just that it must work. In practice, the size is stored somewhere, but we shouldn't bother.
  • Vlad from Moscow
    Vlad from Moscow over 8 years
    @Dr.Programmer Usually it is implemented such a way that the compiler appends a prefix to the memory blcok that contains the the number of elements in the array.