9
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?

cadaniluk
  • 15,027
  • 2
  • 39
  • 67
Dr. Programmer
  • 105
  • 1
  • 1
  • 5

4 Answers4

10

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • How does the delete operator know the size of the array? – Dr. Programmer Oct 25 '15 at 22:25
  • @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. – Bo Persson Oct 25 '15 at 22:36
  • @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. – Vlad from Moscow Oct 25 '15 at 22:37
4

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".
  • 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 Oct 25 '15 at 22:18
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).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 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 Oct 25 '15 at 22:18
  • 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. – Jerry Coffin Oct 25 '15 at 22:20
0

No memory leak is caused since arr is a local variable (and is therefore located on the stack) and goes out of scope when the closing curly brace of main is reached. This implies the absence of the need to call free or delete.

You seem to misunderstand the term "RAM." It just means "Random Access Memory", which can refer to every kind of randomly accessible memory, commonly a PC's main memory. The stack is just as well part of RAM as the free store or (runtime part of ) the OS is.

cadaniluk
  • 15,027
  • 2
  • 39
  • 67
  • 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 Oct 25 '15 at 22:15
  • @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. – cadaniluk Oct 25 '15 at 22:17
  • 1
    @Dr.Programmer http://stackoverflow.com/questions/2589949/c-string-literals-where-do-they-go – cadaniluk Oct 25 '15 at 22:21