How to validate a pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hilary Zhang

    How to validate a pointer

    Hi,

    I have a headache with my program, which is a real-time graphic program
    written in VC++ 6.0. I often get an error of "access violation" when run in
    release mode. But if I use debug mode, it seldom happens since the debug
    mode is much slower than the release mode and the possible bug won't show
    up. I think it may be problem with my usage of pointers. I just wonder is
    there a way to validate a pointer, when it is 0xcdcdcd or 0xffffffff instead
    of NULL(0x000000).

    Thanks in advance.
    Hilary



  • John Harrison

    #2
    Re: How to validate a pointer


    "Hilary Zhang" <hzhang@sfu.c a> wrote in message
    news:c075mr$j01 $1@morgoth.sfu. ca...[color=blue]
    > Hi,
    >
    > I have a headache with my program, which is a real-time graphic program
    > written in VC++ 6.0. I often get an error of "access violation" when run[/color]
    in[color=blue]
    > release mode. But if I use debug mode, it seldom happens since the debug
    > mode is much slower than the release mode and the possible bug won't show
    > up. I think it may be problem with my usage of pointers. I just wonder is
    > there a way to validate a pointer, when it is 0xcdcdcd or 0xffffffff[/color]
    instead[color=blue]
    > of NULL(0x000000).
    >
    > Thanks in advance.
    > Hilary
    >[/color]

    There is no portable way to validate a pointer. This group only discusses
    portable (i.e. standard) C++. You need to ask on a Windows programming
    group. I think Windows has a function called IsValidPtr or something, but
    ask on a Windows group, not here.

    john


    Comment

    • Deming He

      #3
      Re: How to validate a pointer

      Hilary Zhang <hzhang@sfu.c a> wrote in message
      news:c075mr$j01 $1@morgoth.sfu. ca...[color=blue]
      > Hi,
      >
      > I have a headache with my program, which is a real-time graphic program
      > written in VC++ 6.0. I often get an error of "access violation" when run[/color]
      in[color=blue]
      > release mode. But if I use debug mode, it seldom happens since the debug
      > mode is much slower than the release mode and the possible bug won't show
      > up. I think it may be problem with my usage of pointers. I just wonder is
      > there a way to validate a pointer, when it is 0xcdcdcd or 0xffffffff[/color]
      instead[color=blue]
      > of NULL(0x000000).
      >
      > Thanks in advance.
      > Hilary
      >[/color]

      Initialize a pointer to NULL and assign it also NULL when it supposes not
      pointing to somewhere. Then before using, check it against NULL.
      (IsBadCodePtr, IsBadStringPtr, IsBadWritePtr and IsBadStringPtr in MS
      Platform SDK may help you as well.)


      Comment

      • Phlip

        #4
        Re: How to validate a pointer

        Hilary Zhang wrote:
        [color=blue]
        > I have a headache with my program, which is a real-time graphic program
        > written in VC++ 6.0. I often get an error of "access violation" when run[/color]
        in[color=blue]
        > release mode. But if I use debug mode, it seldom happens since the debug
        > mode is much slower than the release mode and the possible bug won't show
        > up. I think it may be problem with my usage of pointers. I just wonder is
        > there a way to validate a pointer, when it is 0xcdcdcd or 0xffffffff[/color]
        instead[color=blue]
        > of NULL(0x000000).[/color]

        Write lots of unit tests to make surprise differences between debug and
        release mode less likely. You can call the offending code more often, in
        more ways, and not worry about performance differences in debug mode.

        NULL all your pointers before use. Don't rely on platforms that write 0s or
        0xDEADBEEF into unused memory before you allocate it. C++ only defines the
        act of sampling the value of a pointer that actually points to a real object
        of its type, or one-off-the-end of an array of said objects, or that
        contains NULL. Your compiler probably does not mind, but the behavior of
        checking if a pointer contains 0xffffffff, unless something of the correct
        type happens to be up there, is undefined.

        Put all pointers into very small objects - call them "handles" - whose only
        job is to manage their pointers. Research "Resource Acquisition is
        Initialization" , and "smart pointers" to learn how.

        Write lots of unit tests to make certain these handles at least present the
        appearance that they work.

        Divide your program in half, and try to reproduce the bug in each half. Take
        the half that has the bug, divide it into quarters, check for the bug, and
        keep going until you have only a small region to search.

        Next time you start a program, don't wait so long before stress-testing.
        Write lots of unit tests as you go, and stress-test these.

        Oh, one more very important tip: Write lots of unit tests.

        --
        Phlip



        Comment

        • Ron Natalie

          #5
          Re: How to validate a pointer


          "John Harrison" <john_andronicu s@hotmail.com> wrote in message news:c079l7$13g vlf$1@ID-196037.news.uni-berlin.de...[color=blue]
          > There is no portable way to validate a pointer. This group only discusses
          > portable (i.e. standard) C++. You need to ask on a Windows programming
          > group. I think Windows has a function called IsValidPtr or something, but
          > ask on a Windows group, not here.[/color]

          The bit patterns he is commenting on are put in there by the debug version for
          otherwise uninitialized and memory returned by free.

          IsValidxxxPtr functions don't tell you if the pointer is valid, just whether you would
          trap if you were to do a read/write access with it.

          Comment

          Working...