Dose this look right??
Code:struct lworlds{ lworlds(); ~lworlds(); char * name; }; struct game{ lworlds world[10]; } lworlds::lworlds() { name = new char[25]; } lworlds::~lworlds() { delete name; }
Dose this look right??
Code:struct lworlds{ lworlds(); ~lworlds(); char * name; }; struct game{ lworlds world[10]; } lworlds::lworlds() { name = new char[25]; } lworlds::~lworlds() { delete name; }
Nope...
If you allocate an array (new something[]) you must destroy it with delete [].Code:lworlds::~lworlds() { delete name; } // should be: lworlds::~lworlds() { delete [] name; }
Making error is human, but for messing things thoroughly it takes a computer
yes, delete the array.
As i see it there are 2 ways, either the name is an array of char* or the name is a single element of an array of classes. Will the same delete[] method work for either situation.
Actually, it's not strictly necessary to call delete with [] for primitives and objects that require no destruction. 'delete name' will free all the memory allocated with 'name = new char[25]'. However if 'name' was an object with a destructor then the [] operator would be required so that the destructor was called for each one. Although, it does no harm calling delete [] on primitives, and it's probably easier to remember to call delete [] whenever you new [].
zen