Do I need to delete basic data types in a destructor? C++
c++, destructor, int, memory
Solution
No.
But not just for basic types. For anything that you didn't allocate with `new` you don't have to call `delete` on. Even for pointers.
`delete` has (somehow) nothing to do with the member variable type. What matters is if you have allocated it (with `new`) in your constructor (or somewhere else in your class methods).
The rule of thumb is
Have as many delete as new
Have as many delete[] as new[].
Of course sometimes you will have to `delete` something you haven't allocated but are pointing to if it is not needed anymore (allocated by someone else but you are the only one using it).
Of course sometimes you will have to NOT `delete` something you HAVE allocated but someone else is pointing to (allocated by you but someone else is using it).
Problem
If I have a class that looks something like this: ``` class SomeClass { public: SomeClass(int size) { arr = new int[size]; someInt = size / 10; }; ~SomeClass() { delete [] arr; //do I need to somehow delete the int value 'someInt'? }; private: int *arr; //pointer to dynamically allocated array int someInt; } ``` What, exactly, should be contained in the destructor to avoid memory leaks? I am aware that I need to delete the array, since it is dynamically allocated, but do I need to do anything with int values, or other basic data types? Thanks, Jonathan