Prabhu
Prabhu

Reputation: 3528

Memory leak/Memory allocation in C++

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?

Upvotes: 3

Views: 844

Answers (8)

Ashish
Ashish

Reputation: 8529

delete is required to call for memory allocated using new ( only for memory obtained from heap ).

Upvotes: 0

MastAvalons
MastAvalons

Reputation: 1141

In order to know exactly whether there is leakage, you can use the debugger. I like to use deleaker. With it you can know at what point there is a leak. And knowing where the leak is easy to remove.

Upvotes: 2

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 247969

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.

Upvotes: 1

Samrat Patil
Samrat Patil

Reputation: 798

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

Upvotes: 4

PP.
PP.

Reputation: 10864

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.

Upvotes: 1

hager
hager

Reputation: 313

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.

Upvotes: 0

Naveen
Naveen

Reputation: 73443

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

Upvotes: 10

sharptooth
sharptooth

Reputation: 170499

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.

Upvotes: 15

Related Questions