Delete pointer to pointer

Sep 21, 2012 at 2:00am
Can I delete int **pRows = new int*[*nrow]; pointer with delete pRows or I must use delete[] pRows?

(I want to just delete the pointer and keep the content!)
Sep 21, 2012 at 2:46am
You must use delete [] whenever you use new[].

(I want to just delete the pointer and keep the content!)


I'm not sure if I understand exactly what you're wanting to do. If you wish to free the memory the pointer uses by using delete, you may not do so. The pointer was not allocated with new. It was made to point to memory allocated by new and that is the memory that would be deallocated with delete.
Sep 21, 2012 at 6:28am
I think you want to delete the pRows pointer only not the content pointed by it.

In this case: it would be automatically deleted/ loose reference when it's scope is gone, but the memory data pointed by pointer would not be deleted; it is the memory leak, as the reference is lost which is not a good practice.
Last edited on Sep 21, 2012 at 6:29am
Sep 21, 2012 at 7:56am
Thanks,

I have saved them in other pointer
Sep 21, 2012 at 8:14am
Just a note on terminology: when someone says "delete a pointer" what they're actually talking about is calling the destructors on whatever it is the pointer points to and marking that memory as free so it can be allocated by other parts of the program/system. They're not talking about destroying the pointer itself.
Thus, this line:
delete p;
is said to be deleting the pointer p.
It can seem counterintuitive, but it's just a way of making sure that what we're saying is directly correlated with what we're writing.

I just thought I'd mention this because it can be a source of confusion.
Sep 21, 2012 at 9:58am
Thanks,
Topic archived. No new replies allowed.