6. Hints for Pointers in PowerBASIC 3.2

Deutsch


Shortindex:
6.1. Pointers in general
6.2. What are pointers, and what are they for?
6.3. PowerBASIC-Pointers and dynamic strings
6.4. PowerBASIC-Pointers and fixed length strings
6.5. PowerBASIC-Pointer and FLEX-Strings
6.6. PowerBASIC-Pointer and TYPE structures
6.7. A little Demonstration (source)

6.1. Pointers in general

Pointers in BASIC have been the cause for many discussions so far. PowerBASICS's Dynamic Memory-Managment has been an argument why Pointers in BASIC (if not impossible) don't make any sense. The following paragraphs will show that this is wrong. Dynamic Memory- Managment (which by the way is a real advantage of this language) and pointers are a possible combination.
For the understanding of the follwing, knowledge of the interna of BASIC and DOS helps a lot

6.2. What are pointers, and what are they for?

Pointers do what their name says: they point. They allow interpreting every single byte of your computers memory up to 1 Megabyte. All you have to do is to assign any memory adress to the pointer. Pointers are useful to get to areas of the memory that are outside of PowerBASIC's Dynamic-Memory- Managment and they let you directly get hold of the Pointers that are given back by some DOS-Functions like: With pointers you can forget all those tiresome "old friends" like DEF SEG/POKE/PEEK or DEF SEG = PEEKI(...)!

6.3. PowerBASIC-Pointers and dynamic strings

String-Pointers to dynamic strings are defined in PowerBASIC with:
        DIM Pointer AS STRING PTR
The Pointer is assigned as follows:
        Pointer = VARPTR32(Demo1$)
    Example:
            '***************************************************************
            '
            ' Demosource showing how to handle pointers and dynamic strings
            '
            '***************************************************************

            DIM Pointer1 AS STRING PTR   ' defining string pointer for
                                         ' dynamic strings
            Pointer1 = VARPTR32(Demo1$)  ' assign the pointer

            CLS
            PRINT "Adress:      Demo1$:       Pointer1:"

            Demo1$ = "123456"
            PRINT HEX$(VARPTR32(Demo1$)), Demo1$, @Pointer1

            Demo1$ = "654321"
            PRINT HEX$(VARPTR32(Demo1$)), Demo1$, @Pointer1

            Demo1$ = "!Test!"
            PRINT HEX$(VARPTR32(Demo1$)), Demo1$, @Pointer1

6.4. PowerBASIC-Pointers and fixed length strings

String-Pointers to fixed strings are defined in PowerBASIC with:
        DIM Demo AS STRING * 6
        DIM Pointer AS STRING PTR * 6
The Pointer is assigned as follows
        Pointer = VARPTR32(Demo$)
    Example:
            '***************************************************************
            '
            ' Demo showing how to handle pointers and fixed length strings
            '
            '***************************************************************

            DIM Demo2 AS STRING * 6        ' define string with fixed length

            DIM Pointer2 AS STRING PTR * 6 ' define Pointer with fixed length
            Pointer2 = VARPTR32(Demo2$)    ' assign the pointer

            PRINT
            PRINT
            PRINT "Adress:      Demo2$:       Pointer2:"

            Demo2$ = "123456"
            PRINT HEX$(VARPTR32(Demo2$)), Demo2$, @Pointer2

            Demo2$ = "654321"
            PRINT HEX$(VARPTR32(Demo2$)), Demo2$, @Pointer2

            Demo2$ = "!Test!"
            PRINT HEX$(VARPTR32(Demo2$)), Demo2$, @Pointer2

6.5. PowerBASIC-Pointer and FLEX-Strings

String-Pointers to FLEX-Strings are defined in PowerBASIC with:
        DIM Demo AS FLEX
        DIM Pointer AS FLEX PTR
The Pointer is assigned as follows
        Pointer1 = VARPTR32(Demo1$)
To use FLEX-Strings you have to use MAP before assigning the Pointer!!
    Example:
            '***************************************************************
            '
            ' Demosource showing how to handle pointers and FLEX-Strings
            '
            '***************************************************************

            DIM Demo3 AS FLEX            ' define string as FLEX!
            DIM Pointer AS FLEX PTR      ' define pointer as FLEX PTR!
            MAP Demo3$$ * 10             '
            FLEXCHR$ = "."

            Pointer = VARPTR32(Demo3$$)  ' assign the pointer

            PRINT
            PRINT
            PRINT "Adress:      Demo3$$:      Pointer:"

            Demo3$$ = "123456"
            PRINT HEX$(VARPTR32(Demo3$$)), Demo3$$, @Pointer

            Demo3$$ = "654321"
            PRINT HEX$(VARPTR32(Demo3$$)), Demo3$$, @Pointer

            Demo3$$ = "!Test!"
            PRINT HEX$(VARPTR32(Demo3$$)), Demo3$$, @Pointer

6.6. PowerBASIC-Pointer and TYPE structures

String-Pointers to TYPE structures are defined in PowerBASIC with:
    TYPE Demo4_Struc                          ' define TYPE
            Demo5  AS BYTE
            Demo6 AS BYTE
    END TYPE
The Pointer is assigned as follows
    DIM TypeDemo AS SHARED Demo4_Struc PTR
You will find an example of pointers and TYPE structures in the following Source.

6.7. A little Demonstration (source)

    '************************************************************************
    '
    ' Handling the Video Ram with pointers in PowerBASIC 3.2
    '
    ' (c) Thomas Gohel
    '
    ' A little demonstration that pointers have really no(!!) problem with
    ' the internal memory-managment. For the successful use of Pointers you
    ' need very good knowledge of the interna of PowerBASIC.
    '
    ' This demo abuses the video-ram as storage for strings and shows how
    ' PRINT commands modify the contents of the two pointers VIDEORAM and
    ' ZEICHEN.
    '
    ' This routine can be used for fast saving and restoring of the complete
    ' video-ram:
    '
    '         @Videoram.Page2 = @Videoram.Page1
    '
    ' will save the complete content of the first (Video)page to the second
    ' page for later restauration.
    '
    '************************************************************************

    TYPE Zeichen_Struc                       ' TYPE of a single sign
            Wert  AS BYTE
            Farbe AS BYTE
    END TYPE

    TYPE Screen_Struc                        ' TYPE for pages of the video ram
            Page1 AS STRING * 4096           ' page 1
            Page2 AS STRING * 4096           ' page 2
            Page3 AS STRING * 4096           ' page 3
            Page4 AS STRING * 4096           ' page 4
    END TYPE

    DIM Zeichen AS SHARED Zeichen_Struc PTR  ' define the TYPE-structur
    DIM Videoram AS SHARED Screen_Struc PTR  ' as pointers

    Videoram = pbvScrnBuff                   ' Move TYPE-structur to the Be-
                                             ' ginning of the video ram.
                                             ' PowerBASIC is using/handling
                                             ' video ram as fixed memory for
                                             ' strings now!  :-)))
    Zeichen  = pbvScrnBuff                   ' TYPE-Structur is to use the
                                             ' same memory-areas as PRINT
                                             ' and VIDEORAM

    SCREEN 0                                 ' set screen-mode
    CLS                                      ' clear screen

    PRINT "Dies ist ein Test"                ' normal PRINT on the
                                             ' screen
    A$=INPUT$(1)

    PRINT LEFT$(@Videoram.Page1,34)          ' show that the PRINT-command
                                             ' has filled our structur
                                             ' too !!
    A$=INPUT$(1)

    @Videoram.Page2 = @Videoram.Page1        ' save page1 to page2

    @Zeichen.Wert  = 76                      ' now ZEICHEN is filled with
                                             ' a value. At the same time
                                             ' the change ist shown on the
    @Zeichen.Farbe = 14                      ' screen and VIDEORAM is
                                             ' modified too !

    PRINT LEFT$(@Videoram.Page1,34)
    A$=INPUT$(1)

    @Videoram.Page1 = @Videoram.Page2        ' restore page1 frome page2


    Pointer_Speed_Test:

            PRINT STRING$(25*80,178);        ' filling the screen
            LOCATE 1, 14
            COLOR 11, 1
            PRINT "  -= STRING-Manipulation inside the";
            PRINT " Video RAM ! =-  "
            @Videoram.Page2 = @Videoram.Page1

            COLOR 14, 1
            LOCATE 8,20: PRINT "ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿"
            FOR i% = 9 TO 18
                LOCATE i%, 20
                PRINT          "³                                        ³"
            NEXT i%
            LOCATE 10, 22: PRINT "    Video RAM is handled as string"
            LOCATE 11, 22: PRINT "          "
            LOCATE 13, 22: PRINT "  -= Demo for the PowerBASIC-FAQ =- "
            LOCATE 19, 20
            PRINT              "ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ"

            @Videoram.Page3 = @Videoram.Page1

            FOR i% = 1 to 1000
                @Videoram.Page1 = @Videoram.Page2
                @Videoram.Page1 = @Videoram.Page3
            NEXT i%

            FOR i% = 1 TO 10
                FOR Durchlauf% = 1 TO 256
                    Zeichen = pbvScrnBuff
                    FOR Offset% = 1 TO 2048
                        IF @Zeichen.Wert > 32 THEN
                            DECR @Zeichen.Wert
                        END IF
                        Zeichen = Zeichen + 2
                    NEXT Offset%
                NEXT Druchlauf%
                @Videoram.Page1 = @Videoram.Page3
            NEXT i%
    '************************************************************************


(c) 1996/2007 by Thomas Gohel, All rights and bugs reserved
(c) 1996/1997 by Wolfgang Bruske, english version