I have created the following class :
class Deep {
private :
int *ptr;
string name;
int age;
double marks;
public :
Deep() : ptr {&age}, name {"Mandeep"}, age {20}, marks {85.3} {
cout<<"Deep Constructor Called for : "<<name<<endl;
}
~Deep() {
delete ptr;
cout<<"Destructor Called for : "<<name<<endl;
}
void get_data() {
cout<<"Name : "<<name<<endl;
cout<<"Age : "<<*ptr<<endl;
cout<<"Marks : "<<marks<<endl;
}
};
int main() {
Deep A;
A.get_data();
cout<<endl<<endl;
return 0;
}
While running the program in Debugger, the program crashes at delete ptr;
.
Is delete ptr;
no necessary here since I am not dynamically allocating any memory to *ptr
?
Is *ptr
already deleted by the destructor and then me trying to delete *ptr
after causing the crash ?