Question : Proper deletion of wchar memroy in C++

Team,

In my program function, I am creating three wchar_t arrays posted below and deleteing them at the end of the function before the function ends, but even after deletion it does not release the memory and continues to eat it up. Can someone tell me what I am doing wrong:

Creation:

wchar_t* char1 = new wchar_t[5000];
wchar_t* char2 = new wchar_t[5000];
wchar_t* char3 = new wchar_t[5000];

Deletion:
char1 = NULL;
char2 = NULL;
char3= NULL;
delete [] char1;
delete [] char2;
delete [] char3;

Answer : Proper deletion of wchar memroy in C++

You want to delete the memory region associated with the new.

wchar_t* char1 = new wchar_t[5000];
delete [] char1;

If the delete operator gets a NULL value to operate on, then it will do nothing. But in the above two lines, the delete operator gets the actual address associated with the new operator, and so will be able to properly deallocate the space created by the new operator.
Random Solutions  
 
programming4us programming4us