Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Right way to delete a pointer

Right way to delete a pointer

Scheduled Pinned Locked Moved C / C++ / MFC
13 Posts 9 Posters 0 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.
  • Z Offline
    Z Offline
    zahid_ash
    wrote on last edited by
    #1

    CSocket * sock; is it a right way to delete a pointer sock = NULL; delete sock; Or can I directly do delete sock; Regards.

    C A S 3 Replies Last reply
    0
    • Z zahid_ash

      CSocket * sock; is it a right way to delete a pointer sock = NULL; delete sock; Or can I directly do delete sock; Regards.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      zahid_ash wrote:

      sock = NULL; delete sock;

      This won't do anything ! You first have to delete the pointer THEN set it to NULL:

      delete sock;
      sock = NULL;


      Cédric Moonen Software developer
      Charting control

      1 Reply Last reply
      0
      • Z zahid_ash

        CSocket * sock; is it a right way to delete a pointer sock = NULL; delete sock; Or can I directly do delete sock; Regards.

        A Offline
        A Offline
        Amar Sutar
        wrote on last edited by
        #3

        CSocket* pSocket; // Allocate a memory for pSocket // Delete a allocated memory if(pSocket) { delete pSocket; pSocket = NULL; } Regards Amar.:)

        V 1 Reply Last reply
        0
        • A Amar Sutar

          CSocket* pSocket; // Allocate a memory for pSocket // Delete a allocated memory if(pSocket) { delete pSocket; pSocket = NULL; } Regards Amar.:)

          V Offline
          V Offline
          Viorel
          wrote on last edited by
          #4

          I think you can even skip the checking for NULL:

          delete pSocket; // without redundant "if(pSocket != NULL) ..."
          pSocket = NULL;

          This is because delete operator performs itself a test for null pointers. You only have to be sure that pSocked was properly initialized before with a right value or with NULL. -- modified at 9:42 Friday 2nd June, 2006

          Z 1 Reply Last reply
          0
          • Z zahid_ash

            CSocket * sock; is it a right way to delete a pointer sock = NULL; delete sock; Or can I directly do delete sock; Regards.

            S Offline
            S Offline
            SteveKing
            wrote on last edited by
            #5

            Always NULL the pointer after deleting it. Otherwise you could (if you're not very, very careful) try to delete the same pointer twice, which will result in an access violation. Deleting a NULL pointer however is perfectly safe.

            J 1 Reply Last reply
            0
            • V Viorel

              I think you can even skip the checking for NULL:

              delete pSocket; // without redundant "if(pSocket != NULL) ..."
              pSocket = NULL;

              This is because delete operator performs itself a test for null pointers. You only have to be sure that pSocked was properly initialized before with a right value or with NULL. -- modified at 9:42 Friday 2nd June, 2006

              Z Offline
              Z Offline
              Zac Howland
              wrote on last edited by
              #6

              Viorel Bejan wrote:

              This is because delete operator performs itself a test for null pointers. You only have to be sure that pSocked was properly initialized before with a right value or with NULL.

              This only happens in debug builds. You should ALWAYS check for null before deleting a pointer. Calling delete NULL has undefined behavior and should be avoided. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

              N N 2 Replies Last reply
              0
              • Z Zac Howland

                Viorel Bejan wrote:

                This is because delete operator performs itself a test for null pointers. You only have to be sure that pSocked was properly initialized before with a right value or with NULL.

                This only happens in debug builds. You should ALWAYS check for null before deleting a pointer. Calling delete NULL has undefined behavior and should be avoided. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                N Offline
                N Offline
                Nish Nishant
                wrote on last edited by
                #7

                Zac Howland wrote:

                This only happens in debug builds. You should ALWAYS check for null before deleting a pointer. Calling delete NULL has undefined behavior and should be avoided.

                That's not true, Zac. See 16.8 on http://www.parashift.com/c++-faq-lite/freestore-mgmt.html[^] Regards, Nish


                Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

                Z 1 Reply Last reply
                0
                • S SteveKing

                  Always NULL the pointer after deleting it. Otherwise you could (if you're not very, very careful) try to delete the same pointer twice, which will result in an access violation. Deleting a NULL pointer however is perfectly safe.

                  J Offline
                  J Offline
                  James R Twine
                  wrote on last edited by
                  #8

                  Another reason for doing that is that the address used for the recently-deallocated memory may still be valid in your address space and while not technically valid for use, accessing it might not cause an IPF or Access Violation.  For example:

                  TCHAR *pcBuffer = new TCHAR[ 1024 ];

                  delete [] pcBuffer;
                  pcBuffer[ 1 ] = _T( 'A' );

                  The above code may not crash even though the pointer is technically invalid.   By setting it to NULL, you just about guarantee that accessing it will cause an Access Violation (at least if on Win32 and if the access range of the pointer is < 4096, because that hits the reserved "NULL pointer page" which causes an instant exception, IIRC).    Peace! -=- James


                  If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                  Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                  DeleteFXPFiles & CheckFavorites (Please rate this post!)

                  1 Reply Last reply
                  0
                  • Z Zac Howland

                    Viorel Bejan wrote:

                    This is because delete operator performs itself a test for null pointers. You only have to be sure that pSocked was properly initialized before with a right value or with NULL.

                    This only happens in debug builds. You should ALWAYS check for null before deleting a pointer. Calling delete NULL has undefined behavior and should be avoided. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                    N Offline
                    N Offline
                    Nemanja Trifunovic
                    wrote on last edited by
                    #9

                    Zac Howland wrote:

                    Calling delete NULL has undefined behavior and should be avoided.

                    Nope, delete NULL is required by the C++ Standard to do nothing. It is perfectly safe.


                    My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

                    1 Reply Last reply
                    0
                    • N Nish Nishant

                      Zac Howland wrote:

                      This only happens in debug builds. You should ALWAYS check for null before deleting a pointer. Calling delete NULL has undefined behavior and should be avoided.

                      That's not true, Zac. See 16.8 on http://www.parashift.com/c++-faq-lite/freestore-mgmt.html[^] Regards, Nish


                      Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
                      Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog (New)

                      Z Offline
                      Z Offline
                      Zac Howland
                      wrote on last edited by
                      #10

                      Yes, the standard says that. However, most compilers did not meet that standard until recently (and some still don't). It is one of those better safe than sorry things. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                      N 1 Reply Last reply
                      0
                      • Z Zac Howland

                        Yes, the standard says that. However, most compilers did not meet that standard until recently (and some still don't). It is one of those better safe than sorry things. If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                        N Offline
                        N Offline
                        Nemanja Trifunovic
                        wrote on last edited by
                        #11

                        Zac Howland wrote:

                        However, most compilers did not meet that standard until recently (and some still don't). It is one of those better safe than sorry things.

                        Compilers don't meet the Standard mostly in some areas of template handling. Deleting a zero is perfectly safe and has been for quite a while.


                        My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

                        Z 1 Reply Last reply
                        0
                        • N Nemanja Trifunovic

                          Zac Howland wrote:

                          However, most compilers did not meet that standard until recently (and some still don't). It is one of those better safe than sorry things.

                          Compilers don't meet the Standard mostly in some areas of template handling. Deleting a zero is perfectly safe and has been for quite a while.


                          My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

                          Z Offline
                          Z Offline
                          Zac Howland
                          wrote on last edited by
                          #12

                          Nemanja Trifunovic wrote:

                          Compilers don't meet the Standard mostly in some areas of template handling. Deleting a zero is perfectly safe and has been for quite a while.

                          Let me put it this way ... There is a reason why the DirectX libraries define the following macro: #define SAFE_DELETE(p) if(p) { delete p; p = NULL; } If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                          N 1 Reply Last reply
                          0
                          • Z Zac Howland

                            Nemanja Trifunovic wrote:

                            Compilers don't meet the Standard mostly in some areas of template handling. Deleting a zero is perfectly safe and has been for quite a while.

                            Let me put it this way ... There is a reason why the DirectX libraries define the following macro: #define SAFE_DELETE(p) if(p) { delete p; p = NULL; } If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                            N Offline
                            N Offline
                            Nemanja Trifunovic
                            wrote on last edited by
                            #13

                            Zac Howland wrote:

                            There is a reason why the DirectX libraries define the following macro

                            Yep, there is: Microsoft DirectX programmers don't know C++ very well. ;P


                            My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups