N/APosted on - 10/19/2014
If we do not use destructor in the code, the program still works fine but I need to know why always we need to write destructor when there is a dynamic memory allocation in program. Does memory leakage occur in this case ? If so then what does default constructor do ?
Want to user destructor working in C++
Compilers by default will introduce default constructors and destructors when it is not declared explicitly. This is the normal behavior.
Usage of default constructor and destructor is that they will automatically allocate and deallocate required memory for the members which is associated with it.
But, in case of the member which is of pointer type, the above explanation is not valid. So when we consider memory leakage, there will be cases where leakage happens. To avoid such memory leakage it is mandatory to provide default constructors and destructors explicitly and memory management should as well be explicitly handled.