memory - Do I need to delete basic data types in a destructor? C++ -
if have class looks this:
class someclass { public: someclass(int size) { arr = new int[size]; someint = size / 10; }; ~someclass() { delete [] arr; //do need somehow delete int value 'someint'? }; private: int *arr; //pointer dynamically allocated array int someint; } what, exactly, should contained in destructor avoid memory leaks?
i aware need delete array, since dynamically allocated, need int values, or other basic data types?
thanks, jonathan
no.
but not basic types. didn't allocate new don't have call delete on. even pointers.
delete has (somehow) nothing member variable type. matters if have allocated (with new) in constructor (or somewhere else in class methods).
the rule of thumb
have many delete new have many delete[] new[]. of course have delete haven't allocated pointing if not needed anymore (allocated else 1 using it).
of course have not delete have allocated else pointing (allocated else using it).
Comments
Post a Comment