Announcement

Collapse
No announcement yet.

string pointers

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

  • Guest's Avatar
    Guest replied
    Lance,

    Thanks again. You have been great!

    Pat

    Leave a comment:


  • Lance Edmonds
    replied
    First thing to note is that STRING PTR and STRPTR32 are not the same thing - this point often confuses people. (Note: The STRPTR and STRSEG functions provide the same information as STRPTR32 but the latter returns a single 32-bit address rather than separate 16-bit segment and offset values. For simplicity and clarity, I will not discuss these in this discussion).

    Ok, to understand how a dynamic string pointer works, you have to understand how dynamic strings are stored in memory: Each dynamic string consists of the data contained within the string, and also a "string handle". Basically, the string handle is used by the PowerBASIC string engine to identify the location of the actual target data that the string contains.

    With these points in mind, we can examine the handle or the data by using pointers, but we need to use the correct type of pointer for each:

    1. We use STRPTR32() to return a 32-bit pointer to the actual string data held by the string. One common use is to use a BYTE PTR to hold the value, and then the contents of the string data can be read on a byte-by-byte basis.

    2. We use VARPTR32() to return a 32-bit pointer to the string handle. String handle pointers are stored in STRING PTR variables.

    Why would we every need a pointer to the handle? Simple. We can often substitute a pointer target in the place of a dynamic strings in our code.

    OK, this can be confusing so, lets look at some example code to clarify the situation:

    Code:
    cls
     
    DIM a AS STRING
    DIM b AS STRING PTR
    DIM c AS BYTE PTR
    DIM d as integer
    a = CHR$(34) + "A PowerBASIC Dynamic String!" + CHR$(34)
    PRINT "The original string is " a
    b = VARPTR32(a)
    PRINT "The pointer is located at " HEX$(b)
    PRINT "The target of the pointer is " @b
    PRINT
    
    PRINT "Now lets look at the data in the string itself:"
    c = STRPTR32(@b) ' we could also use c = STRPTR32(a)
    PRINT "The string data is located at " HEX$(c)
    PRINT "The data (byte-by-byte) is ";
    FOR d = 1 to LEN(@b)
       PRINT CHR$(@c);
       incr c
    NEXT
    PRINT 
     
    PRINT
    PRINT "Now lets do that again using indexed pointers:"
    PRINT "The data (byte-by-byte) is ";
    c = STRPTR32(a) ' we could also use STRPTR32(@b)
    FOR d = 0 to LEN(a) - 1
       PRINT CHR$(@c[d]);
    NEXT
    PRINT : PRINT
    PRINT "Clear as mud?"
    What this code shows is that we reference the string handle with VARPTR32() and access the string content with STRPTR32(). It is often every handy to be able to substitute a pointer to a string handle, rather than use a string directly. For example:

    Code:
    DIM a AS STRING
    DIM b AS STRING PTR
    
    a = CHR$(34) + "A PowerBASIC Dynamic String!" + CHR$(34)
    b = VARPTR32(a)
    PRINT MyFunction(a)
    print MyFunction(@b)
    
    FUNCTION MyFunction(x as string) as string
        FUNCTION = "Received = " + x
    END FUNCTION
    NOTES:
    1. These techniques work almost identically in PB/CC and PB/DLL (Our Windows compilers use VARPTR instead of VARPTR32, and STRTR instead of STRPTR32, but otherwise the methods are identical).
    2. Only dynamic strings use a string handle... fixed-length and ASCIIZ occupy fixed storage in memory and therefore VARPTR32 returns a pointer to the string data in fixed-length & ASCIIZ strings.
    3. If a dynamic string is nul (ie, zero length), then STRPTR32 will return an "invalid" pointer - the programmer is responsible for ensuring memory addressed by invalid or null pointers is not altered otherwise data-corruption and subtle bugs in seemingly unrelated parts in your code can occur. At worst, yo can crash the operating system. Always use case when dealing with pointers of all kinds!



    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>

    Leave a comment:


  • Guest's Avatar
    Guest started a topic string pointers

    string pointers

    Does anyone know how to use pointers to strings? I find the
    manual very skimpy on this. Can anyone give me an example? Thanks in advance.

    Pat
Working...
X