I want to dynamically resize the size of an array of chars.
What is the problems with this code ? I got an error on the line "delete[] arr_items[i];"
char **arr_items; int items=0;
void AddItem(const char* text) {
if (items>0) {
char** tmp = new char*[items+1];
std::copy(arr_items, arr_items + items, tmp);
tmp[items] = new char[MAX_LABEL_LEN];
strncpy(tmp[items], text, MAX_LABEL_LEN);
for (int i=0; i<items; i++) {
delete[] arr_items[i];
}
delete[] arr_items;
arr_items = tmp;
}
else {
arr_items = new char*[1];
arr_items[0] = new char[MAX_LABEL_LEN];
strncpy(arr_items[items], text, MAX_LABEL_LEN);
}
items++;
}
I don't want to use vector, please refrain, if you know how to do it in the old style, tell me, if not, look elsewhere. The question is clear, if you don't know, keep silence pls!