I am still trying to understand why do I have to use the brackets when deleting an array. and no brackets other wise.
CornedBee wrote :
It's got, among other things, something to do with destructors. Only with [] does delete know to call more than one destructor. (Which doesn't give you permission to mix them if the underlying object doesn't have a destructor.)
So I wrote this little example, and understod part of it.
Code:#include <iostream> #include <mylib.h> #define N 3 class mstr { char *str; public : mstr(void) { str = NULL; } void add_new(const char *s) { str = new char[strlen(s)+1]; strcpy(str, s); } ~mstr(void) { if ( str ) { printf("delete %s\n", str); delete str; } } void show(void) { printf("%s\n", str); } }; class strt { public : mstr *tbl; strt(void) { tbl = new mstr[N]; tbl[0].add_new("strt :one"); tbl[1].add_new("strt :two"); tbl[2].add_new("strt :three"); } void disp_tbl(void) { for ( int i = 0 ; i < N ; ++i ) tbl[i].show(); } }; int main(void) { strt st1, st2; int *p = new int[5]; delete p; // I dont use bracktes and dont crash here !!!! st1.disp_tbl(); printf("===> delete []st1.tbl <=== : \n"); delete []st1.tbl; printf("===> delete st2.tbl <=== : \n"); delete st2.tbl; // I dont use bracktes and DO crash here !!!! return(0); }
I see that when I call "delete []st1.tbl" , with brackets it also call
delete for mstr[0] mstr[1] mstr[2].
But why does the program crash with "delete st2.tbl" ?
The output is :
strtne
strt :two
strt :three
===> delete []st1.tbl <=== :
delete strt :three
delete strt :two
delete strtne
===> delete st2.tbl <=== :
delete strtne
Abort (core dumped)



LinkBack URL
About LinkBacks
ne


