Is there a need to destroy char * = "string" or char * = new char[6]?
No. You only need to manually free strings when you manually allocate the memory yourself using the malloc
function (in C) or the new
operator (in C++). If you do not use malloc
or new
, then the char*
or string will be created on the stack or as a compile-time constant.
No. When you say:
const char* c = "Hello World!";
You are assigning c to a "pre-existing" string constant which is NOT the same as:
char* c = new char[6];
Only in the latter case are you allocating memory on the heap. So you'd call delete when you're done.
I assume when I do
char* = "string"
its the same thing aschar* = new char[6]
.
No. What the first one does is create a constant. Modifying it is undefined behaviour. But to answer your question; no, you don't have to destroy them. And just a note, always use std::string
whenever possible.