I have the following function in C++
void func1()
{
char *p = "Test for memory leak";
}
When func1()
is called where is the memory for the variable allocated? Whether in the stack or the heap? Should delete p;
be called explicitly?
I have the following function in C++
void func1()
{
char *p = "Test for memory leak";
}
When func1()
is called where is the memory for the variable allocated? Whether in the stack or the heap? Should delete p;
be called explicitly?
Memory for the string literal is allocated in static storage and that allocation duration is for the whole program running time. You should not call delete - calling delete will lead to undefined behaviour.
No, the memory is allocated only for the pointer p
on the stack. This memory is automatically reclaimed when p
goes out of scope. p
is just pointing to a string which is stored some where in the read-only section of your program. Ideally it should be defined as const char *p
. If you try to delete
it, it will be undefined behavior. As a rule, you can remember that for every call to new
there needs to be a call of delete
String literals are typically located in the read only text segment of your executable. Calling free/delete on them would probably lead to bad things.
There is no memory leak at all. If you look at the compiled code the string Test for memory leak\0
is actually part of the executable program - and the loader will copy this into memory during execution. The operating system clears up this pre-loaded memory upon program termination.
The variable *p
itself is allocated on the stack when the function is called and when the function returns the pointer is removed from the stack.
delete is required to call for memory allocated using new ( only for memory obtained from heap ).
new and new[] operators are used to explicitly allocate memory on the heap in C++.
The rule is
1. call delete
for every new
operator used
2. call delete[]
for every new[]
operator used.
The rest everything goes on stack and one shouldn't deallocate it explicitly. It'll automatically be taken care of.
Follow this rule and you won't go wrong.
Although, beware, if you're using new in a loop and using delete outside it. It causes big memory leaks.
Best practice is to use smart pointers which automatically deallocate memory for you as the pointer goes out of scope. Boost library offers some good options for the same. Read more on it here:http://www.boost.org/doc/libs/1_41_0/libs/smart_ptr/smart_ptr.htm
--Samrat Patil
The function defines a pointer p
, which is set to point to the statically allocated string "Test for memory leak"
.
Nothing is being dynamically allocated, so nothing has to be manually freed.
You should always pair calls to new
and delete
. When something is new
'ed, it must be deleted
, and vice versa.
In your case, the string itself is static, and lasts until the program terminates.
And p
is a local variable on the stack, and it lasts until the function returns.
So both of these are handled automatically by the system.