arrays - Deleting dynamic char** in C++ -
disclosure: i'm trying solve challenge strict time , memory limits. use vectors , strings, here need fastest , smallest solution (with vectors ran above time limit), turned dynamic arrays of char*. relevant parts of code:
char** substrings(string s, int* n){ *n = 0; ... //////////////////////////////// char** strings = new char*[*n]; //////////////////////////////// (int = 0; < s.length(); i++){ (int j = 1; j < s.length() - + 1; j++){ ... strings[si] = tmp; ... } } return strings; } int main(){ ... (int ti = 0; ti < t; ti++){ cin >> s; char** substr = substrings(s, &n); ... (int = 0; < n; i++){ delete substr[i]; } } return 0; } everything runs fine without deleting array (of arrays), unacceptable, how go this? i've tried lot of variations seemed logical runtime errors.
it similar allocating, in reverse order, , using delete[] instead of new[]:
for(int = 0; < length; i++) delete[] strings[i]; // delete each pointer in char** strings delete[] strings; // delete array of pointers i assumed here length length of array of pointers char*. looks perform first round of de-allocation
for (int = 0; < n; i++){ delete substr[i]; // need delete[] substr[i] here but delete instead of delete[], need delete[] substr[i] instead, guess substr[i] char* pointer pointing first element of array of chars allocated new[]. need additional
delete[] substr;
Comments
Post a Comment