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 know if a pointer is valid?

How to know if a pointer is valid?

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
12 Posts 6 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.
  • H Offline
    H Offline
    hatemtalbi
    wrote on last edited by
    #1

    Hey, does someone know how to check if a pointer is valid (was allocated by the application and can be used without problem) or no?

    D V S R 4 Replies Last reply
    0
    • H hatemtalbi

      Hey, does someone know how to check if a pointer is valid (was allocated by the application and can be used without problem) or no?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Have you looked into IsBadCodePtr(), IsBadReadPtr(), IsBadWritePtr(), or IsBadStringPtr()?


      "The largest fire starts but with the smallest spark." - David Crow

      "Judge not by the eye but by the heart." - Native American Proverb

      S 1 Reply Last reply
      0
      • H hatemtalbi

        Hey, does someone know how to check if a pointer is valid (was allocated by the application and can be used without problem) or no?

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

        In Windows you can try IsBadReadPtr, IsBadWritePtr and IsBadStringPtr. In MFC you can try AfxIsValidAddress and AfxIsValidString. In case of run-time library, you can try _CrtIsValidPointer, _CrtIsValidHeapPointer and _CrtIsMemoryBlock.

        H 1 Reply Last reply
        0
        • H hatemtalbi

          Hey, does someone know how to check if a pointer is valid (was allocated by the application and can be used without problem) or no?

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

          Best programs always handle the pointers validity by checking NULL or not. for this you should cautious about allocation and de-allocation SaRath.
          "Do Next Thing..." My Blog | Understanding State Pattern in C++

          1 Reply Last reply
          0
          • V Viorel

            In Windows you can try IsBadReadPtr, IsBadWritePtr and IsBadStringPtr. In MFC you can try AfxIsValidAddress and AfxIsValidString. In case of run-time library, you can try _CrtIsValidPointer, _CrtIsValidHeapPointer and _CrtIsMemoryBlock.

            H Offline
            H Offline
            hatemtalbi
            wrote on last edited by
            #5

            AfxIsValidAddress worked fine for me. Thanks a lots :)

            S 1 Reply Last reply
            0
            • H hatemtalbi

              Hey, does someone know how to check if a pointer is valid (was allocated by the application and can be used without problem) or no?

              R Offline
              R Offline
              Roland Pibinger
              wrote on last edited by
              #6

              hatemtalbi wrote:

              does someone know how to check if a pointer is valid (was allocated by the application and can be used without problem) or no?

              When you ask that question something is wrong in your code. You should never have to check if a pointer is 'valid'.

              S 1 Reply Last reply
              0
              • D David Crow

                Have you looked into IsBadCodePtr(), IsBadReadPtr(), IsBadWritePtr(), or IsBadStringPtr()?


                "The largest fire starts but with the smallest spark." - David Crow

                "Judge not by the eye but by the heart." - Native American Proverb

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

                You are asking for trouble if you rely on these APIs to tell if memory is safe to use. For example with heaps when you free a block it doesn't mean the memory is deallocated straight away; it can be marked as free and recycled. This is how heaps work. In short if IsBadReadPtr returns TRUE you know for sure that the memory isn't safe to read; but if it returns FALSE it does ***NOT*** mean that the memory is safe to use - it could be on the heap's free list for example. These API are only meant for debugging purposes and not for memory management in the manner your post alludes to. Steve

                D 1 Reply Last reply
                0
                • H hatemtalbi

                  AfxIsValidAddress worked fine for me. Thanks a lots :)

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

                  You are asking for trouble if you rely on these APIs to tell if memory is safe to use. For example with heaps when you free a block it doesn't mean the memory is deallocated straight away; it can be marked as free and recycled. This is how heaps work. For example: if IsBadReadPtr returns TRUE you know for sure that the memory isn't safe to read; but if it returns FALSE it does ***NOT*** mean that the memory is safe to read - it could be on the heap's free list for example. Most people don't believe when I tell them this. The following code shows this effect in action: int *pInt = new int; delete pInt; ASSERT( AfxIsValidAddress(pInt, sizeof(int)) ); If this practice was safe the ASSERT should fire: it doesn't. The memory is being cached in the heap but it is definitely ***NOT*** safe to use it. DO NOT USE THIS TECHNIQUE! Steve

                  D 1 Reply Last reply
                  0
                  • R Roland Pibinger

                    hatemtalbi wrote:

                    does someone know how to check if a pointer is valid (was allocated by the application and can be used without problem) or no?

                    When you ask that question something is wrong in your code. You should never have to check if a pointer is 'valid'.

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

                    I agree 100%. You get a five for making so much sense. Steve

                    1 Reply Last reply
                    0
                    • S Stephen Hewitt

                      You are asking for trouble if you rely on these APIs to tell if memory is safe to use. For example with heaps when you free a block it doesn't mean the memory is deallocated straight away; it can be marked as free and recycled. This is how heaps work. For example: if IsBadReadPtr returns TRUE you know for sure that the memory isn't safe to read; but if it returns FALSE it does ***NOT*** mean that the memory is safe to read - it could be on the heap's free list for example. Most people don't believe when I tell them this. The following code shows this effect in action: int *pInt = new int; delete pInt; ASSERT( AfxIsValidAddress(pInt, sizeof(int)) ); If this practice was safe the ASSERT should fire: it doesn't. The memory is being cached in the heap but it is definitely ***NOT*** safe to use it. DO NOT USE THIS TECHNIQUE! Steve

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      Stephen Hewitt wrote:

                      If this practice was safe the ASSERT should fire: it doesn't.

                      Actually, it should only fire if the address pointed to by pInt was not contained entirely within the program’s memory space.


                      "The largest fire starts but with the smallest spark." - David Crow

                      "Judge not by the eye but by the heart." - Native American Proverb

                      S 1 Reply Last reply
                      0
                      • S Stephen Hewitt

                        You are asking for trouble if you rely on these APIs to tell if memory is safe to use. For example with heaps when you free a block it doesn't mean the memory is deallocated straight away; it can be marked as free and recycled. This is how heaps work. In short if IsBadReadPtr returns TRUE you know for sure that the memory isn't safe to read; but if it returns FALSE it does ***NOT*** mean that the memory is safe to use - it could be on the heap's free list for example. These API are only meant for debugging purposes and not for memory management in the manner your post alludes to. Steve

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        Stephen Hewitt wrote:

                        These API are only meant for debugging purposes and not for memory management in the manner your post alludes to.

                        Hence my question. :)


                        "The largest fire starts but with the smallest spark." - David Crow

                        "Judge not by the eye but by the heart." - Native American Proverb

                        1 Reply Last reply
                        0
                        • D David Crow

                          Stephen Hewitt wrote:

                          If this practice was safe the ASSERT should fire: it doesn't.

                          Actually, it should only fire if the address pointed to by pInt was not contained entirely within the program’s memory space.


                          "The largest fire starts but with the smallest spark." - David Crow

                          "Judge not by the eye but by the heart." - Native American Proverb

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

                          DavidCrow wrote:

                          Actually, it should only fire if the address pointed to by pInt was not contained entirely within the program’s memory space.

                          Yes, that's what the API does. This doesn't contradict my statement. The OP wanted to know how to tell if memory "was allocated by the application and can be used without problem". AfxIsValidAddress was put forward as a solution and the OP accepted it claiming that it worked. I pointed out that if this API could be used to perform this function then the ASSERT in the code below should fire as AfxIsValidAddress should return FALSE given that it is surely not safe to use a pointer after it has been deleted: int *pInt = new int; delete pInt; ASSERT( AfxIsValidAddress(pInt, sizeof(int)) ); This is not the case. If you try this code AfxIsValidAddress returns TRUE and thus the ASSERT will not fire. Why? Because the address pointed to by pInt was entirely contained in the process’s address space. It's cached by the heap (the small block heap in this case) for recycling. So the problem is that just because an address is physically mapped into the process's address space doesn't mean it's safe to use. In general these APIs can only be used to tell if memory can't be safely used but not to tell if it is safe to use. Steve

                          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