Deleting memory from function C++ <p>I'm having trouble freeing my memory I'm using and a little confused how I would go about doing it. When I do it with the code below i get an error "Heap Corruption Detected... CRT detected that the application wrote to memory after end of heap buffer". I also debugged to make sure there is a memory leak using the Crtdb and there is a leak on that variable. Just am confused how to free it.</p> <pre><code>#include &lt;iostream&gt; #include &lt;string.h&gt; using namespace std; void adjustVar(char*&amp; pointer, size_t i) { pointer = new char[i]; } int main(void) { const char* org = "HELLOWORLD"; char* p = nullptr; size_t size = strlen(org); adjustVar(p, size); memset(p, 0, size+1); strncpy(p, org, size); cout &lt;&lt; p &lt;&lt; endl; delete[] p; return 0; } </code></pre>