I assume that char* = "string"
is the same to char* = new char[6]
. I believe these strings are created on the heap instead of the stack. So do I need to destroy them or free their memory when I'm done using them or do they get destroyed by themselves?

- 10,252
- 6
- 30
- 51

- 13,470
- 24
- 79
- 81
8 Answers
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.

- 19,397
- 6
- 54
- 74

- 9,615
- 6
- 33
- 38
-
I think that string literal is allocated in `.rodata` section not in the stack. – NAND Aug 09 '20 at 18:48
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.
The name of the game is "destroy only what you created". Here are the pairs:
malloc
/free
calloc
/free
new
/delete
new []
/delete []
Since you created the 2nd string using new []
, the onus is on you to destroy it with delete []
. Call delete [] string2
when you are done.
Now if your code is convoluted enough and makes keeping track of deletions difficult, consider the usage of scoped pointers or auto pointers. The boost::scoped_ptr
class from boost library is a good place to begin. Also look into the RAII idiom, pretty handy and useful stuff.

- 19,397
- 6
- 54
- 74

- 3,378
- 2
- 28
- 51
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.

- 19,397
- 6
- 54
- 74

- 45,296
- 18
- 54
- 69
They're not the same. Your first example is a constant string, so it's definitely not allocated from the heap. Your second example is a runtime memory allocation of 6 characters, and that comes from the heap. You don't want to delete your first example, but you need to delete []
your second example.

- 19,397
- 6
- 54
- 74

- 11,894
- 12
- 69
- 85
-
1"new" allocates memory from the heap, never from the stack. (Unless, of course, you have overridden the new operator.) – Jim Buck Sep 09 '08 at 11:07
-
Let's see what GCC 4.8 x86-64 Linux does
Program:
#include <cstdio>
int main() {
const char *s = "abc";
char *sn = new char[4];
sn[3] = '\0';
std::printf("%s\n", s);
std::printf("%s\n", sn);
}
Compile and decompile:
g++ -c -ggdb -o a.o -std=c++98 a.cpp
objdump -CSr a.o
The output contains:
const char *s = "abc";
8: 48 c7 45 f0 00 00 00 movq $0x0,-0x10(%rbp)
f: 00
c: R_X86_64_32S .rodata
char *sn = new char[4];
10: bf 04 00 00 00 mov $0x4,%edi
15: e8 00 00 00 00 callq 1a <main+0x1a>
16: R_X86_64_PC32 operator new[](unsigned long)-0x4
1a: 48 89 45 f8 mov %rax,-0x8(%rbp)
Interpretation:
char *s = "abc"
goes into.rodata
. So you cannotfree
it in any way.char *sn = new char[4];
comes from the output ofoperator new[]
. So you should free it when you can.

- 347,512
- 102
- 1,199
- 985
You don't know where the string literals are stored. It may even be read-only memory, so your code should read:
const char* c = "string";
And a new char array should be deleted just like any other dynamically allocated memory area.

- 45,516
- 10
- 73
- 79
new is always an allocation whereas defining a string inline actually embeds the data in the program itself and cannot be changed (some compilers allow this by a smart trick, don't bother).
Some compilers type inline strings so that you cannot modify the buffer.
char* const sz1 = "string"; // embedded string, immutable buffer
char* sz2 = new char[10]; // allocated string, should be deleted

- 35,564
- 14
- 82
- 119