delete[] or delete or delete[] * ???

From: pembed2003 (
Date: 03/31/04

Date: 30 Mar 2004 23:23:42 -0800

I have function that allocates a bunch of char* like:

int stuffIt(char*** s){
        *s = new char*[3];
        **s = new char[4];
        *(*s+1) = new char[4];
        *(*s+2) = new char[4];
        **s = "abc";
        *(*s+1) = "123";
        *(*s+2) = "yyy";
        return 3;

It's call like:

char** stuff;
int size = stuffIt(&stuff);

I then verify it with:

for(int i = 0; i < size; i++){

which correctly prints out "abc","123","yyy" but how do I delete the
allocated memory when I am done? I try:

delete[] stuff; // doesn't generate any error

which only deletes the 3 char* but doesn't actually delete each
individual char* right? So I tried delete each one by saying:

for(int j = 0; j < size; j++){
   delete[] stuff[j];

but that generate a bunch of errors. I also tried something like:

delete stuff[j];
delete [] *(*stuff+j);
delete [] stuff+j;
delete [] (*stuff+j);

but none of them work. Any idea?

Relevant Pages