Appropriate way to delete this pointer

heaveneye
heaveneye used Ask the Experts™
on
It happened that I need to manage some other people's source code. In the source, I found that a pointer it was instantiated as below was not freed after being used:

TCHAR** val= NULL;
val = new TCHAR*[MAX_NB];

However, I am unsure the appropriate way to free up the pointer. I am wondering the followings:

delete val[]; // <----- is this appropriate? or should it be "delete val[][]" ?
val=NULL;

I am quite young on c++ programing, and need some advice from experts.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You should use:

free(val);

This will free the memory and set tour pointer to NULL.
Commented:
delete [] val;
it is array of TCHAR* - each element is a pointer.
Your example with modifications to demonstrate:

#include <tchar.h>
#include <stdio.h>
int main()
{
   TCHAR** val = NULL;
    val = new TCHAR*[12];
   val[0] = new TCHAR[24];
   _sntprintf(val[0], 20, _T("%d %s"), 20090721, _T("date"));
   _tprintf(_T("string %s"), val[0]);
   delete [] (val[0]);
    delete [] val;
   return 0;
}
 

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial