Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. An easy question on Pointer
Forum Updated to NodeBB v4.3 + New Features

An easy question on Pointer

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 865 Views 1 Watching
  • 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.
  • mrdebugM Offline
    mrdebugM Offline
    mrdebug
    wrote on last edited by
    #1

    I have a main class where I create an Object
    @QTSClient= new QTcpSocket();@
    After that I pass the pointer to a function
    @void Call(QTcpSocket *QTSClient= NULL);@
    in the Call function I delete and NULL the pointer like this:
    @delete QTSClient;
    QTSClient= NULL;@
    Really, the QTSClient is deletet but, in the main class, the QTSClient object results to be deleted but not = NULL.
    Can you explain to me why?

    Need programmers to hire?
    www.labcsp.com
    www.denisgottardello.it
    GMT+1
    Skype: mrdebug

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      Why should it be NULL?

      A pointer is simply a memory address. So you are passing the memory address, which is stored in the QTSClient variable, into the sub-function. And you are passing it "by value", not "by reference". Inside the sub-function, you delete the object at that address. Now the value of QTSClient points to some invalid address, because the object no longer exists! This is called a "dangling" pointer. Inside the sub-function you also set the local variable QTSClient to NULL. But why should the variable QTSClient in the main function have become NULL too?

      It obviously doesn't. It still contains the old (now invalid) address!

      --

      What you could do is passing a pointer to the pointer:
      @void Call(QTcpSocket **QTSClient)
      {
      delete (*QTSClient);
      *QTSClient= NULL;
      }@

      And then call it like this:
      @main()
      {
      QTSClient = new QTcpSocket();
      Call(&QTSClient);
      }@

      --

      Yet another option is using Smart-Pointers instead of "raw" pointers...

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • mrdebugM Offline
        mrdebugM Offline
        mrdebug
        wrote on last edited by
        #3

        @ void Call(QTcpSocket **QTSClient)
        {
        delete (*QTSClient);
        *QTSClient= NULL;
        }@

        Perfect!!

        Need programmers to hire?
        www.labcsp.com
        www.denisgottardello.it
        GMT+1
        Skype: mrdebug

        1 Reply Last reply
        0

        • Login

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