c++ - Proper way of deleting a char** array -
i have dynamically allocated char**
array private member in 1 of classes.
the first allocation occurs according number of words
client_interests = new char* [n];
later each index of array allocated according length of word + 1
char[i] = new char [strlen(word)+1];
would proper way of deallocating memory of member (the classes dtor calling function)?
void client::deallocate() { int i; (i = 0; < n; ++) //loops through each word { delete [] client_interests[i]; //each word array of characters, hence delete [] used } delete [] client_interests; //deallocating pointer client_interests = null; }
thaks!
your code correct. since allocate using new []
, de-allocating delete []
required. also, de-allocating in reverse order - inner arrays first, outer array - required.
Comments
Post a Comment