Announcement

Collapse

Maintenance

The forum could be offline for 30-60 minutes in the very near future for maintenance (said 3pm Pacific). I was behind on getting this notice. I do apologize.
See more
See less

Need help with pointers

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

  • Need help with pointers

    My first pointer experience in PB/DOS 3.50 *g*... but what am I doing wrong? I'm stuck!
    Please help me - any hint would be greatly appreciated...

    Thanks!

    Heinz Salomon

    Code:
    $DIM ALL
    $ERROR ALL ON
    DEFINT A-Z
    
    DECLARE SUB ShowSortArrayElement(BYVAL WORD)
    DECLARE FUNCTION HexAddr$(BYVAL WORD)
    
    DIM i AS INTEGER, wElement AS WORD
    
    $If 0
      ===========================================================
        IDEA
      ===========================================================
        (1) uTestType.fArrayPtr() points to the 2-byte elements
            of the fixed length string array fSortArray().
        -------------------------------------------------------
        (2) fSortArray is sorted using the ARRAY SORT command.
        -------------------------------------------------------
        (3) The sorted contents of fSortArray() should then be 
            accessed via the uTestType.fArrayPtr() Pointer.
      ===========================================================
        SCREEN OUTPUT
      ===========================================================
        FILL & SORT SORTARRAY
    
          fSortArray()'s contents before sorting -->
            01020304050607080910
          fSortArray()'s contents after sorting -->
            10090807060504030201            [<--- OK!]
    
        ACCESS fSortArray() WITH PEEK & PTR
    
          SortArray(1) -->
            PEEK @ 1B65:0000 = 10           [<--- OK!]
             PTR @ 1B65:0000 =  459603968   [<--- ???]
    
          SortArray(2) -->
            PEEK @ 1B65:0002 = 09           [<--- OK!]
             PTR @ 1B65:0002 =  138085      [<--- ???]
    
          SortArray(3) -->
            PEEK @ 1B65:0004 = 08           [<--- OK!]
             PTR @ 1B65:0004 =  459603970   [<--- ???]
           
          [and so on]
        =========================================================
    $EndIf
    
    TYPE TestTypeStruc
      tName AS STRING * 30
      yAge AS BYTE
      fArrayPtr (1 TO 10) AS STRING PTR * 2
    END TYPE
    DIM uTestType AS SHARED TestTypeStruc 
    
    CLS
    PRINT "FILL & SORT SORTARRAY"
    PRINT
    
    DIM fSortArray(1 TO 10) AS SHARED STRING * 2
    DIM fSortArrayPtr AS SHARED STRING PTR * 20
    fSortArrayPtr = VARPTR32(fSortArray(1))
    @fSortArrayPtr = "01020304050607080910"
    PRINT "  fSortArray()'s contents before sorting --> "
    PRINT "    "; @fSortArrayPtr
    
    ARRAY SORT fSortArray(), DESCEND
    PRINT "  fSortArray()'s contents after sorting --> "
    PRINT "    "; @fSortArrayPtr
    PRINT
    
    LSET uTestType.tName = "Gruber,Fritz"
    uTestType.yAge = 45
    
    PRINT "ACCESS fSortArray() WITH PEEK & PTR"
    PRINT
    
    uTestType.fArrayPtr(1) = fSortArrayPtr
    FOR i = 2 TO 10
      uTestType.fArrayPtr(i) = uTestType.fArrayPtr(i - 1) + 2
    NEXT
    
    FOR wElement?? = 1 TO 10
      ShowSortArrayElement wElement??
    NEXT
    
    WHILE NOT INSTAT: WEND
    
    END
    
    SUB ShowSortArrayElement(BYVAL rwElement AS WORD)
      PRINT "  SortArray(" & LTRIM$(STR$(rwElement)); ") --> "
      DEF SEG = VARSEG(fSortArray(1))
      PRINT "    PEEK @ " ; _
        HexAddr$(VARSEG(fSortArray(rwElement??))) ; ":" ; _
        HexAddr$(VARPTR(fSortArray(rwElement??))); " = "; _
        PEEK$(VARPTR(fSortArray(rwElement??)), 2)
      DEF SEG
      PRINT "     PTR @ "; _
        HexAddr$(uTestType.fArrayPtr(rwElement??) \ 65536); ":"; _
        HexAddr$(uTestType.fArrayPtr(rwElement??) MOD 65536); " = "; _
        [email protected](rwElement??)
      PRINT
    END SUB
    
    FUNCTION HexAddr$(BYVAL rwAddress AS WORD)
      HexAddr$ = RIGHT$("000" & HEX$(rwAddress??), 4)
    END FUNCTION


    [This message has been edited by Heinz Salomon (edited August 31, 2006).]

  • #2
    Originally posted by Heinz Salomon:
    Please help me - any hint would be greatly appreciated...
    Heinz, your code is right but is not compiled in a good way even
    at plain DOS. Try change the variables names to other that do not
    ressemble the reserved words of BASIC. I think that those variables
    are confusing the compiler. Try rearrange the order of yours subs
    and functions.

    Best Regards,

    Arthur.


    ------------------
    "The trouble with quotes on the Internet is that you can never know if they are genuine." - Abraham Lincoln.

    Comment


    • #3
      Code:
      FOR i = 2 TO 10 
       uTestType.fArrayPtr(i) = uTestType.fArrayPtr(i - 1) + 2
      NEXT
      Not quite sure what you are trying here, but should't this loop execute 10 times instead of 9?

      MCM

      Michael Mattias
      Tal Systems Inc. (retired)
      Racine WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        PowerBASIC gets "confused" because identifiers resemble reserved words? The compiler generates an error because it doesn't like the color of your shirt?

        Both are equally plausible. {smile} You must look elsewhere for the answer.


        Best regards,

        Bob Zale
        PowerBASIC Inc.


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

        Comment


        • #5
          @Arthur
          I thought I could use reserved basic words in variable names as long as they are only part of the name?

          @MCM
          Code:
          uTestType.fArrayPtr(1) = fSortArrayPtr
          FOR i = 2 TO 10
            uTestType.fArrayPtr(i) = uTestType.fArrayPtr(i - 1) + 2
          NEXT
          The address contained in fSortArrayPtr (points to the first element of fSortArray()) is first assigned to uTestType.fArrayPtr(1), then the value of the previous array element (1 to 9) is assigned to the remaining elements of the array (2 to 10), each incremented by 2 (should then point to the next array element in the fixed string array).

          Regards
          Heinz Salomon



          [This message has been edited by Heinz Salomon (edited September 02, 2006).]

          Comment


          • #6
            Originally posted by Bob Zale:
            {smile}
            Bob, when everything more fails...

            Give us a light!

            Best Regards,

            Arthur.

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


            [This message has been edited by Arthur Gomide (edited September 02, 2006).]
            "The trouble with quotes on the Internet is that you can never know if they are genuine." - Abraham Lincoln.

            Comment


            • #7

              Heinz, sees what I obtained

              FOR I=1 TO 10
              PRINT uTestType.fArrayPtr(I); [email protected](I)
              NEXT I

              1961951232 1961951232
              1961951234 161009
              1961951236 1961951234
              1961951238 292081
              1961951240 1961951236
              1961951242 423153
              1961951244 1961951238
              1961951246 554225
              1961951248 1961951240
              1961951250 685297

              ???

              Best Regards,

              Arthur.


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


              [This message has been edited by Arthur Gomide (edited September 02, 2006).]
              "The trouble with quotes on the Internet is that you can never know if they are genuine." - Abraham Lincoln.

              Comment


              • #8
                This issue becomes more and more mysterious to me...

                [email protected](rwElement??) should evaluate to a STRING * 2 value - but if you pass the pointer target to a function which expects a string argument, as in VAL([email protected](rwElement??)), you get the compile time error "421: String operand expected". Obviously, [email protected](rwElement??) - though declared as STRING PTR * 2 - evaluates to a numeric value. Therefore you can pass it to the HEX$() function which leads to the following - interesting! - results:

                Code:
                ACCESS fSortArray() WITH PEEK & PTR
                
                  fSortArray(1) -->
                    PEEK @ 1B68:0000 = 10
                     PTR @ 1B68:0000 =  459800576; HEX$(@PTR) = 1B680000
                
                  fSortArray(2) -->
                    PEEK @ 1B68:0002 = 09
                     PTR @ 1B68:0002 =  138088; HEX$(@PTR) = 21B68
                
                  fSortArray(3) -->
                    PEEK @ 1B68:0004 = 08
                     PTR @ 1B68:0004 =  459800578; HEX$(@PTR) = 1B680002
                ???
                The pointer address appears - sometimes shifted - where you would expect the STRING * 2 pointer target??

                I am absolutely clueless what happens here.

                Please help!

                Regards
                Heinz Salomon

                [This message has been edited by Heinz Salomon (edited September 02, 2006).]

                Comment


                • #9
                  What exactly is this code trying to do?

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

                  Comment


                  • #10
                    I'm not sure if you're seeing a bug in PowerBASIC or a weakness in the pointer syntax. The latter looks to be the case. If I create a temporary variable of type STRING PTR * 2, then assign it the value of the array item, I can then use the @ operator to dereference the temporary pointer and I get the right print out.

                    From my experience, pointers in PowerBASIC (for DOS) are very weak. They pretend to operators (in the same sense as an arithmatic operator) but in fact are not. In the ideal world, any expression that evaluates to the right kind of pointer should be able to be dereferenced. Unfortunately in PB, the dereference operator has to be next to a variable name. And when you add in arrays and user-defined types to the mix, the behavior seems undefined. I would welcome clarification on the issue.

                    In any event, try the temporary variable as a work-around.

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

                    Comment


                    • #11
                      You hit the nail on the head. Thanks for the hint Michael! Using a temporary pointer array instead of the pointer array within the type structure solves the problem.

                      I'd too welcome clarification. I personally don't see much difference between a weakness in the pointer syntax and a bug. As long as I obey to the language rules as described in the manuals I'd expect to get the correct results - no matter in which of the thousand possible ways I combine or nest the language elements.

                      If I got something wrong and misunderstood the POWERBasic language rules, I'd like to learn about that. I am not the type of programmer who lacks skill and experience and blames the programming language for that.

                      Regards
                      Heinz Salomon

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

                      Comment


                      • #12
                        My problem, condensed and rephrased:
                        Code:
                        $DIM ALL
                        $ERROR ALL ON
                        $LIB IPRINT OFF
                        DEFINT A-Z
                        
                        DIM i AS INTEGER
                        DIM fStringArray(1 TO 10) AS STRING * 2
                        DIM fRegularArrayPtr(1 TO 10) AS STRING PTR * 2
                        
                        TYPE TestTypeStruc
                          fTypeArrayPtr(1 TO 10) AS STRING PTR * 2
                          fTypePtr AS STRING PTR * 2
                        END TYPE
                        DIM uTestType AS TestTypeStruc 
                        
                        'assign values from "01" to "10" to fStringArray()
                        FOR i = 1 TO 10
                          fStringArray(i) = MID$(STR$(100 + i), 3)
                        NEXT
                        
                        'point to each array element
                        FOR i = 1 TO 10
                          uTestType.fTypeArrayPtr(i) = VARPTR32(fStringArray(i))
                          fRegularArrayPtr(i) = VARPTR32(fStringArray(i))
                        NEXT
                        uTestType.fTypePtr = VARPTR32(fStringArray(5))
                        
                        'output the string array elements in several ways
                        CLS
                        PRINT
                        
                        PRINT "  ACCESSING THE ELEMENTS OF fStringArray():"
                        PRINT
                        PRINT
                        PRINT "    ... within a loop counting the subscripts up from 1 to 10:"
                        PRINT
                        PRINT "      "; _
                          "fStringArray()"; TAB(25); _
                          "@fRegularArrayPtr()"; TAB(48); _
                          "[email protected]()"
                        PRINT 
                        
                        FOR i= 1 TO 10
                          PRINT "      "; _
                            fStringArray(i); TAB(25); _
                            @fRegularArrayPtr(i); TAB(48); _
                            [email protected](i)
                        NEXT
                        
                        PRINT
                        PRINT
                        PRINT "    ... via [email protected](), but using constants as subscripts:"
                        PRINT
                        PRINT "      "; [email protected](1)
                        PRINT "      "; [email protected](2)
                        PRINT "      "; [email protected](3)
                        PRINT "      "; [email protected](4)
                        PRINT "      "; [email protected](5)
                        PRINT "      "; [email protected](6)
                        PRINT "      "; [email protected](7)
                        PRINT "      "; [email protected](8)
                        PRINT "      "; [email protected](9)
                        PRINT "      "; [email protected](10)
                        PRINT
                        PRINT
                        PRINT "    ... via [email protected] (points to array element #5):"
                        PRINT
                        PRINT "      "; [email protected]
                        
                        WHILE NOT INSTAT: WEND
                        
                        END

                        Output:

                        Code:
                          ACCESSING THE ELEMENTS OF fStringArray():
                        
                        
                            ... within a loop counting the subscripts up from 1 to 10:
                        
                              fStringArray()    @fRegularArrayPtr()    [email protected]()
                        
                              01                01                      440401920
                              02                02                      137792
                              03                03                      440401922
                              04                04                      268864
                              05                05                      440401924
                              06                06                      399936
                              07                07                      440401926
                              08                08                      531008
                              09                09                      440401928
                              10                10                      662080
                        
                        
                            ... via [email protected](), but using constants as subscripts:
                        
                              01
                              ë
                              02
                              :\
                              03
                              (Ç
                              04
                              Î'
                              05
                              @►
                        
                        
                            ... via [email protected] (points to array element #5):
                        
                              05
                        Conclusion:
                        Are pointer arrays illegal within TYPE structures?

                        Regards
                        Heinz Salomon

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

                        Comment


                        • #13
                          >Are pointer arrays illegal within TYPE structures?

                          That "arrays" within types are not really ARRAYs - there is no array descriptor and none of the ARRAY statements may be used against them - may be part of the explanation.


                          Michael Mattias
                          Tal Systems Inc. (retired)
                          Racine WI USA
                          [email protected]
                          http://www.talsystems.com

                          Comment


                          • #14
                            Could a PowerBasic employee please comment on my problem?
                            Is there a solution?

                            Thank you!

                            Regards
                            Heinz Salomon

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

                            Comment


                            • #15
                              In the PowerBASIC DOS compiler, arrays of pointer variables within a User-Defined Type are not supported for direct addressing (though it is in our Windows compilers). At the present time, we'd suggest you use an alternate method depending upon the exact circumstances of your code.

                              Best regards,

                              Bob Zale
                              PowerBASIC Inc.


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

                              Comment


                              • #16
                                So when you say "unsupported," you mean the behavior is undefined (random essentially)? I don't expect anything to be done about this, but maybe a syntax error would have been nice rather than bizarre and unexplained results as he was getting. Therefore, this is, in my opinion a PB/DOS compiler bug.

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


                                [This message has been edited by Michael Torrie (edited September 09, 2006).]

                                Comment

                                Working...
                                X