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. How to verify a pointer is valid? [modified]

How to verify a pointer is valid? [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
8 Posts 8 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.
  • F Offline
    F Offline
    flyingxu
    wrote on last edited by
    #1

    class CSomeObject : public CObject { //... }; CSomeObject *pObject = new CSomeObject; CSomeObject *pSave = pObject; //.. //some other operation, the pObject may be deleted or not //but pSave don't know what happened But now I want to use the pointer of pSave, can I verify if pSave is valid or not before I use it? try/catch? or any method else? -- modified at 23:26 Tuesday 13th June, 2006

    S N G G L 6 Replies Last reply
    0
    • F flyingxu

      class CSomeObject : public CObject { //... }; CSomeObject *pObject = new CSomeObject; CSomeObject *pSave = pObject; //.. //some other operation, the pObject may be deleted or not //but pSave don't know what happened But now I want to use the pointer of pSave, can I verify if pSave is valid or not before I use it? try/catch? or any method else? -- modified at 23:26 Tuesday 13th June, 2006

      S Offline
      S Offline
      Sarath C
      wrote on last edited by
      #2

      Actually new returns '0' if it is unsuccessful. you can use IsBadWritePtr or IsBadReadPtr to validate the pointer. new will throw exception if it fails, u can also catch for that exception See MSDN for more details SaRath.
      "Don't Do Different things... Do Things Differently..." Understanding State Pattern in C++

      S 1 Reply Last reply
      0
      • F flyingxu

        class CSomeObject : public CObject { //... }; CSomeObject *pObject = new CSomeObject; CSomeObject *pSave = pObject; //.. //some other operation, the pObject may be deleted or not //but pSave don't know what happened But now I want to use the pointer of pSave, can I verify if pSave is valid or not before I use it? try/catch? or any method else? -- modified at 23:26 Tuesday 13th June, 2006

        N Offline
        N Offline
        Nibu babu thomas
        wrote on last edited by
        #3

        flyingxu wrote:

        But now I want to use the pointer of pSave, can I verify if pSave is valid or not before I use it? try/catch? or any method else?

        Look up _set_new_handler, _set_new_mode, _query_new_handler, AfxSetNewHandler (this is not documented). The above functions transfers control to your error-handling mechanism if the new operator fails to allocate memory. These functions (except for _set_new_mode and _query_new_handler) take a function pointer as an argument and returns the old one. The _query_new_handler function returns the address of the current new handler. Make sure you set the old one back before exiting.

        PNH old_handler = _set_new_handler( my_handler );
        // Code that requires my_handler
        _set_new_handler( old_handler )
        // Code that requires old_handler


        Nibu thomas A Developer Programming tips[^]  My site[^]

        1 Reply Last reply
        0
        • F flyingxu

          class CSomeObject : public CObject { //... }; CSomeObject *pObject = new CSomeObject; CSomeObject *pSave = pObject; //.. //some other operation, the pObject may be deleted or not //but pSave don't know what happened But now I want to use the pointer of pSave, can I verify if pSave is valid or not before I use it? try/catch? or any method else? -- modified at 23:26 Tuesday 13th June, 2006

          G Offline
          G Offline
          grigsoft
          wrote on last edited by
          #4

          Of course, it is offtopic, but I would say that requirement to verify if pointer is good or not is a result of bad objects intraction design. There never should be invalid pointers, because you will forget to verify it once - either now or when you will have to modify your code one year later. Igor Green http://www.grigsoft.com/ - files and folders comparison tools

          1 Reply Last reply
          0
          • S Sarath C

            Actually new returns '0' if it is unsuccessful. you can use IsBadWritePtr or IsBadReadPtr to validate the pointer. new will throw exception if it fails, u can also catch for that exception See MSDN for more details SaRath.
            "Don't Do Different things... Do Things Differently..." Understanding State Pattern in C++

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            Using IsBadWritePtr and friends will not solve the OP's problem. IsBadWritePtr can return FALSE even after an object as been deleted as heap blocks can be marked as free but the memory still be allocated. On top of this the CRT can add another level of caching. In short, if IsBadWritePtr returns TRUE you know something is wrong and it's not safe to use the memory, but if returns FALSE it does not necessarily mean all is well. Steve

            1 Reply Last reply
            0
            • F flyingxu

              class CSomeObject : public CObject { //... }; CSomeObject *pObject = new CSomeObject; CSomeObject *pSave = pObject; //.. //some other operation, the pObject may be deleted or not //but pSave don't know what happened But now I want to use the pointer of pSave, can I verify if pSave is valid or not before I use it? try/catch? or any method else? -- modified at 23:26 Tuesday 13th June, 2006

              G Offline
              G Offline
              Ganesh_T
              wrote on last edited by
              #6

              Check this link[^] Cheers "Peace of mind through Technology"

              1 Reply Last reply
              0
              • F flyingxu

                class CSomeObject : public CObject { //... }; CSomeObject *pObject = new CSomeObject; CSomeObject *pSave = pObject; //.. //some other operation, the pObject may be deleted or not //but pSave don't know what happened But now I want to use the pointer of pSave, can I verify if pSave is valid or not before I use it? try/catch? or any method else? -- modified at 23:26 Tuesday 13th June, 2006

                L Offline
                L Offline
                Laxman Auti
                wrote on last edited by
                #7

                flyingxu wrote:

                But now I want to use the pointer of pSave, can I verify if pSave is valid or not before I use it? try/catch?

                Use ASSERT in Debug mode B'coz it works in Debug mode only. Knock out 't' from can't, You can if you think you can :cool:

                1 Reply Last reply
                0
                • F flyingxu

                  class CSomeObject : public CObject { //... }; CSomeObject *pObject = new CSomeObject; CSomeObject *pSave = pObject; //.. //some other operation, the pObject may be deleted or not //but pSave don't know what happened But now I want to use the pointer of pSave, can I verify if pSave is valid or not before I use it? try/catch? or any method else? -- modified at 23:26 Tuesday 13th June, 2006

                  T Offline
                  T Offline
                  Tim Paaschen
                  wrote on last edited by
                  #8

                  You may use a smart pointer that implements reference counting, e.g. boost::shared_ptr, instead of raw pointer.

                  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