我分配了内存,但是当我调用析构函数时,它给了我一个分段错误。这是代码。我是否使用正确的方法来释放内存?
class plan {
char *symbol;
gro *grow;
public:
plan (int, char[] ); //constructor
~plan ( ); //destructor
};
plan::plan (int num_of_sm, char sm[]){
try {
symbol = new char [strlen(sm) + 1];
}
catch (std::bad_alloc) {
symbol = NULL;
}
if (symbol != NULL) {
if (sm == NULL) {
strcpy (symbol, "");
}
else {
strcpy (symbol, sm);
}
}
gro = new grow [num_of_sm];
}
plan::~plan( ){
delete [ ] symbol;
delete [ ] gro;
}