How to free an array in C/C++
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:
- No, there are no memory leaks here.
- 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 usingmalloc
ornew
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).
Dr. Programmer
Updated on October 26, 2020Comments
-
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; }
Does this code create a memory leak?
Where does
arr
reside? On the stack or in RAM?
-
Ivan over 8 yearsC and C++ are two completely different languages. Which one are you using?
-
Dr. Programmer over 8 yearsThank'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 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 over 8 yearsThank'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 over 8 yearsThank'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 over 8 yearsAssuming 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 over 8 years@Dr.Programmer stackoverflow.com/questions/2589949/…
-
Dr. Programmer over 8 yearsHow does the delete operator know the size of the array?
-
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 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.