Explicitly deleting char pointer within object
Posted on 2003-11-05
I have a char pointer within a object definition that I am allocating with 'new' in one of the object methods. Should I (be able to) deallocate the memory via delete within the destructor? The reading I have done suggests any time I allocate with 'new' I need to also deallocate with delete, but I seem to run into alot of incidences where my program chokes when I explicitly delete. It chokes at the point of the delete cmd.
char *val_label; // Label for this value
value(char * stringPassed) : val_label(NULL)
delete val_label; // sometimes will bomb here, sometimes not?
char * label(const char *stringPassed)
if (val_label) delete val_label;
val_label = new char[strlen(stringPassed)+1];
void main ()
value test = new value("mytestlabel");