Announcement

Collapse
No announcement yet.

What is the minimum value of a pointer?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • What is the minimum value of a pointer?

    What is the minimum value of a pointer?

    The reason for this question is that i would like an element in a UDT to be an integer ID OR a pointer to an ID-string:

    Code:
    type MYTYPE
        ....
        ID as long   'Integer ID OR a pointer to an ID-string
        ...
    end type
    In the function that has the UDT as input i have to have a way to analyze the ID. If i know %MIN_POINTER_VALUE i can do this:
    Code:
    function MyFunc(mt as MYTYPE) as long
    
        select case mt.ID
        case < %MIN_POINTER_VALUE
            ' The integer-id case
        case else        
            ' The string-id case     
        end select
    
    end function
    Regards
    Peter

    [This message has been edited by Peter Stephensen (edited June 18, 2000).]

  • #2
    Probably, I'm wrong, but I don't beleive that Ptr could be < 1 Mb (at least, 640K ... 1Mb)

    ------------------

    Comment


    • #3
      Peter

      I don't think you'll find a reliable way to do what you want using a minumum pointer value. Why don't you do something like:

      Code:
      #COMPILE EXE
      
      %LONG_LONG = 1
      %LONG_PTR = 2
      %DOUBLE_PTR = 3
      %INTEGER_PTR = 4
      %ASCIIZ_PTR = 5
      
      
      UNION U_TYPES
            lValue AS LONG
            plValuePtr AS LONG PTR
            pdValuePtr AS DOUBLE PTR
            piValuePtr AS INTEGER PTR
            pszValuePtr AS ASCIIZ PTR
      END UNION
      
      TYPE T_TYPES
           lValueType AS LONG '%LONG_LONG, %LONG_PTR, %DOUBLE_PTR, etc
           uType AS U_TYPES
      END TYPE
            
      
      FUNCTION PBMAIN() AS LONG
         LOCAL sTest AS STRING
         LOCAL tTest AS T_TYPES
         
         
         sTest = "HELLO THERE"
         
         tTest.uType.lValue = STRPTR(sTest)
         tTest.lValueType = %ASCIIZ_PTR
         
         SELECT CASE tTest.lValueType
                CASE %LONG_LONG
                     PRINT tTest.uType.lValue
                CASE %LONG_PTR
                     PRINT [email protected]
                CASE %DOUBLE_PTR
                     PRINT [email protected]
                CASE %INTEGER_PTR
                     PRINT [email protected]
                CASE %ASCIIZ_PTR
                     PRINT [email protected]
         END SELECT
         
         
         WAITKEY$
      END FUNCTION
      Florent

      ------------------

      Comment


      • #4
        There is a simple solution:
        Code:
        #Include "WIN32API.INC"
        type MYTYPE    ....    
          ID as long   'Integer ID OR a pointer to an ID-string    
        End type
        
        Function MyFunc(mt as MYTYPE) as long    
        If IsBadCodePtr (mt.ID) Then
        ' do whatever....(it's not a pointer or an invalid one)
        Else
        ' do whatever....(it's a pointer)
        End If
        ------------------
        Kind regards,
        Peter.



        [This message has been edited by Peter Lameijn (edited June 18, 2000).]
        Regards,
        Peter

        "Simplicity is a prerequisite for reliability"

        Comment


        • #5
          Florent --
          From one side you are right, but from another side look, what Windows does.
          There is a lot API functions similar LoadIcon.
          Code:
          HICON LoadIcon(
            HINSTANCE hInstance, // handle to application instance
            LPCTSTR lpIconName   // name string or resource identifier
          );
          Parameters
          hInstance
          [in] Handle to an instance of the module whose executable file contains the icon to be loaded. This parameter must be NULL when a standard icon is being loaded.
          lpIconName
          [in] Pointer to a null-terminated string that contains the name of the icon resource to be loaded. Alternatively, this parameter can contain the resource identifier in the low-order word and zero in the high-order word. Use the MAKEINTRESOURCE macro to create this value.

          This means that Windows automatic accepts that a value <= &H0000FFFF is ID.


          ------------------

          Comment


          • #6
            Peter, good one - that'll do the trick!

            Semen, interesting comment - I'll have a think about that one 8-)

            Cheers

            Florent

            ------------------

            Comment


            • #7
              Peter --
              That's it !

              Semen --
              Your comment suggests an alternative method:
              Code:
              function MyFunc(mt as MYTYPE) as long
               
                  if hiwrd(mt.ID) then
                      ' The string-id case     
                  else        
                      ' The integer-id case
                  end if
               
              end function
              Regards
              Peter

              ------------------


              [This message has been edited by Peter Stephensen (edited June 18, 2000).]

              Comment


              • #8
                Guys --
                IsBadCodePtr checks the read access at the specified address.
                Nothing more.
                I agree than to use it much better than the minimum value of a pointer from the sky, but test following
                (reduce - if necessary %Max)
                Code:
                #Compile Exe
                #Include "win32api.inc"
                %Max = 100000000 '&HFFFFFFFF&
                Function PbMain
                   Dim i As Dword, j As Dword
                   Do
                      i = j
                      Do
                         If i = %Max Then Exit Do
                         If IsBadCodePtr(i) = %False Then MsgBox Format$(i),, "Start": Exit Do
                         Incr i
                      Loop
                      If i = %Max Then Exit Do
                      j = i
                      Do
                         If j = %Max Then Exit Do
                         If IsBadCodePtr(j) = %True Then MsgBox Format$(j - 1),, "Finish": Exit Do
                         Incr j
                      Loop
                      If j = %Max Then Exit Do
                   Loop
                
                End Function
                On my PC under Win95b this code returns a lot of ranges.
                (I guess, including DLL memory, drivers and so on).

                ------------------

                Comment


                • #9
                  A "pointer" is a blob that points to an actual data item. It is not a good idea to construe a pointer as something for which a "minimum value" has any meaning. It's a wrong question!

                  There are no guarantees as to the range of values that a pointer may have. It's arbitrary and may change depending on the operating system, other programs that are loaded, or the phase of the moon.

                  The IsBadxxxPtr functions are not going to do the trick for you here. They only check to see whether the pointer points to a currently-valid area of memory. Obviously, some data values may randomly turn out to point to valid areas of memory. Worse, of course, the valid areas will change as your program allocates and deallocates memory, so the same value may be differently interpreted at different points in the program!

                  This is just not a right way of going about it. I'd suggest adding a flag byte or some other explicit "mode" value to your Type.

                  ------------------
                  Tom Hanlin
                  PowerBASIC Staff

                  Comment

                  Working...
                  X