To add to keskiverto's response, you should delete it when ptr goes out of scope. Because the function could throw an exception anywhere, you need this to happen automatically.
Thanks both of you for reply.
@Repeater
As you said by some reason if we want to make it on heap and still dont use smart pointer.
You mean,
If exception occurred, Then there is not other way to delete this object?
I have read about RAII on google, And happy to understand the concept.
Below post found very clear on RAII:
"RAII is the design paradigm to ensure that variables handle all needed initialization in their constructors and all needed cleanup in their destructors. This reduces all initialization and cleanup to a single step.
C++ does not require RAII, but it is increasingly accepted that using RAII methods will produce more robust code.
The reason that RAII is useful in C++ is that C++ intrinsically manages the creation and destruction of variables as they enter and leave scope, whether through normal code flow or through stack unwinding triggered by an exception. That's a freebie in C++.
By tying all initialization and cleanup to these mechanisms, you are ensured that C++ will take care of this work for you as well.
Talking about RAII in C++ usually leads to the discussion of smart pointers, because pointers are particularly fragile when it comes to cleanup. When managing heap-allocated memory acquired from malloc or new, it is usually the responsibility of the programmer to free or delete that memory before the pointer is destroyed. Smart pointers will use the RAII philosophy to ensure that heap allocated objects are destroyed any time the pointer variable is destroyed."