Memory leak/Memory allocation in C++
c++, memory-leaks, memory-management, visual-c++
Solution
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`
Problem
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?