Search code examples
c++memorynew-operatordelete-operator

Does memory get freed at the end of a function if you don't use delete?


Say you have a function like:

void foo()
{
  char* pt = new char[10];
  //do stuff with pt
}

Since the pointer was created locally, will the memory be freed once the function terminates? Or do you really need to use delete[] to free the memory?


Solution

  • Memory allocated with new/new[] have dynamic storage duration. They are not deallocated until the user explicitly calls delete/delete[].