<p>I assume that <code>char* = "string"</code> is the same to <code>char* = new char[6]</code>. 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?</p>

[{'answer_id': 51595, 'author': 'aib', 'author_id': 1088, 'author_profile': 'https://Stackoverflow.com/users/1088', 'pm_score': 1, 'selected': False, 'text': '<p>You don\'t know where the string literals are stored. It may even be read-only memory, so your code should read:</p>\n\n<pre><code>const char* c = "string";\n</code></pre>\n\n<p>And a <em>new</em> char array should be <em>delete</em>d just like any other dynamically allocated memory area.</p>\n'}, {'answer_id': 51598, 'author': 'Andrew', 'author_id': 826, 'author_profile': 'https://Stackoverflow.com/users/826', 'pm_score': 2, 'selected': False, 'text': "<p>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 <code>delete []</code> your second example.</p>\n"}, {'answer_id': 51599, 'author': 'Bernard', 'author_id': 61, 'author_profile': 'https://Stackoverflow.com/users/61', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>I assume when I do <code>char* = "string"</code> its the same thing as <code>char* = new char[6]</code>. </p>\n</blockquote>\n\n<p>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 <code>std::string</code> whenever possible.</p>\n'}, {'answer_id': 51602, 'author': 'Mike McQuaid', 'author_id': 5355, 'author_profile': 'https://Stackoverflow.com/users/5355', 'pm_score': 6, 'selected': True, 'text': '<p>No. You only need to manually free strings when you manually allocate the memory yourself using the <code>malloc</code> function (in C) or the <code>new</code> operator (in C++). If you do not use <code>malloc</code> or <code>new</code>, then the <code>char*</code> or string will be created on the stack or as a compile-time constant.</p>\n'}, {'answer_id': 51607, 'author': 'Vincent Robert', 'author_id': 268, 'author_profile': 'https://Stackoverflow.com/users/268', 'pm_score': 0, 'selected': False, 'text': '<p>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).</p>\n\n<p>Some compilers type inline strings so that you cannot modify the buffer.</p>\n\n<pre><code>char* const sz1 = "string"; // embedded string, immutable buffer\nchar* sz2 = new char[10]; // allocated string, should be deleted\n</code></pre>\n'}, {'answer_id': 51608, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': False, 'text': '<p>No. When you say:</p>\n\n<pre><code>const char* c = "Hello World!";\n</code></pre>\n\n<p>You are assigning c to a "pre-existing" string constant which is NOT the same as:</p>\n\n<pre><code>char* c = new char[6];\n</code></pre>\n\n<p>Only in the latter case are you allocating memory on the heap. So you\'d call delete when you\'re done.</p>\n'}, {'answer_id': 3693866, 'author': 'Fanatic23', 'author_id': 350810, 'author_profile': 'https://Stackoverflow.com/users/350810', 'pm_score': 2, 'selected': False, 'text': '<p>The name of the game is "destroy only what you created". Here are the pairs:</p>\n\n<ol>\n<li><code>malloc</code>/<code>free</code></li>\n<li><code>calloc</code>/<code>free</code></li>\n<li><code>new</code>/<code>delete</code></li>\n<li><code>new []</code>/<code>delete []</code></li>\n</ol>\n\n<p>Since you created the 2nd string using <code>new []</code>, the onus is on you to destroy it with <code>delete []</code>. Call <code>delete [] string2</code> when you are done.</p>\n\n<p>Now if your code is convoluted enough and makes keeping track of deletions difficult, consider the usage of scoped pointers or auto pointers. The <a href="http://www.boost.org/doc/libs/1_44_0/libs/smart_ptr/scoped_ptr.htm" rel="nofollow noreferrer"><code>boost::scoped_ptr</code></a> class from boost library is a good place to begin. Also look into the <a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization" rel="nofollow noreferrer">RAII</a> idiom, pretty handy and useful stuff.</p>\n'}, {'answer_id': 30666555, 'author': 'Ciro Santilli OurBigBook.com', 'author_id': 895245, 'author_profile': 'https://Stackoverflow.com/users/895245', 'pm_score': 1, 'selected': False, 'text': "<p><strong>Let's see what GCC 4.8 x86-64 Linux does</strong></p>\n<p>Program:</p>\n<pre><code>#include &lt;cstdio&gt;\nint main() {\n    const char *s = &quot;abc&quot;;\n    char *sn = new char[4];\n    sn[3] = '\\0';\n    std::printf(&quot;%s\\n&quot;, s);\n    std::printf(&quot;%s\\n&quot;, sn);\n}\n</code></pre>\n<p>Compile and decompile:</p>\n<pre><code>g++ -c -ggdb -o a.o -std=c++98 a.cpp\nobjdump -CSr a.o\n</code></pre>\n<p>The output contains:</p>\n<pre><code>  const char *s = &quot;abc&quot;;\n 8:   48 c7 45 f0 00 00 00    movq   $0x0,-0x10(%rbp)\n f:   00 \n                     c: R_X86_64_32S .rodata\n  char *sn = new char[4];\n10:   bf 04 00 00 00          mov    $0x4,%edi\n15:   e8 00 00 00 00          callq  1a &lt;main+0x1a&gt;\n                        16: R_X86_64_PC32       operator new[](unsigned long)-0x4\n1a:   48 89 45 f8             mov    %rax,-0x8(%rbp)\n</code></pre>\n<p>Interpretation:</p>\n<ul>\n<li><code>char *s = &quot;abc&quot;</code> goes into <code>.rodata</code>. So you cannot <code>free</code> it in any way.</li>\n<li><code>char *sn = new char[4];</code> comes from the output of <code>operator new[]</code>. So you should free it when you can.</li>\n</ul>\n"}]
['https://Stackoverflow.com/questions/51592', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1688440/']