Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Segmentation Fault on object Creation
QtWS25 Last Chance

Segmentation Fault on object Creation

Scheduled Pinned Locked Moved Unsolved C++ Gurus
constructor
3 Posts 3 Posters 740 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vinoth Rajendran4
    wrote on 30 Dec 2019, 12:05 last edited by
    #1

    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

    J 1 Reply Last reply 30 Dec 2019, 12:30
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 30 Dec 2019, 12:23 last edited by
      #2

      Because it's an infinite recursion - with every creation of a Hello object you create a new one.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • V Vinoth Rajendran4
        30 Dec 2019, 12:05

        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

        J Offline
        J Offline
        JonB
        wrote on 30 Dec 2019, 12:30 last edited by JonB
        #3

        @Vinoth-Rajendran4
        Further to @Christian-Ehrlicher, in other words that means you need to delete the statement Hello* obj = new Hello;! Your calling code will go new Hello, and that will execute your constructor and return the created instance, you don't want another new Hello anywhere inside your Hello class as it stands.

        1 Reply Last reply
        4

        1/3

        30 Dec 2019, 12:05

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved