Segmentation Fault on object Creation
-
wrote on 30 Dec 2019, 12:05 last edited by
Hello All,
class Hello { public: Hello* obj = new Hello; inline Hello() { cout << "Constructor"; } inline ~Hello() { cout << "Destructor"; } };
When i create an object of Hello , i am getting segmentation fault.
Can someone please help he understand the inner working on why segmentation fault happens.
Any help is deeply appreciated. Thanks in advance
-
Because it's an infinite recursion - with every creation of a Hello object you create a new one.
-
Hello All,
class Hello { public: Hello* obj = new Hello; inline Hello() { cout << "Constructor"; } inline ~Hello() { cout << "Destructor"; } };
When i create an object of Hello , i am getting segmentation fault.
Can someone please help he understand the inner working on why segmentation fault happens.
Any help is deeply appreciated. Thanks in advance
wrote on 30 Dec 2019, 12:30 last edited by JonB@Vinoth-Rajendran4
Further to @Christian-Ehrlicher, in other words that means you need to delete the statementHello* obj = new Hello;
! Your calling code will gonew Hello
, and that will execute your constructor and return the created instance, you don't want anothernew Hello
anywhere inside yourHello
class as it stands.
3/3