Announcement

Collapse
No announcement yet.

Pointers vs DEF SEG

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

  • Walt Decker
    replied
    Thanks, Lance. That clears it up. I'd forgotten about strptr32.

    Walt

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

    Leave a comment:


  • Lance Edmonds
    replied
    Both strategies are correct, but your implementation of the pointers not correct, and this is giving you "wrong" results.

    The important thing to remember is that a STRPTR and a STRING POINTER are not exactly the same thing.

    The problem in your example code is that you are assigning the address of a string handle to a byte pointer. Therefore when you dereference the pointer (ie, @intaptr), you are picking up the 1st byte of the string handle, not the string data.

    In this case you need to use intaptr = STRPTR32(five).

    The rules are a bit confusing, but you need to remember the following:
    • STRPTR32() - returns a pointer to the string data.
    • VARPTR32() - returns a pointer to the string handle.
    • DIM x AS STRING PTR - a string pointer requires the address of a string handle
    • DIM x AS BYTE PTR - to use indirection (pointers) to manipulate the data held by a string, assign it STRPTR32() address of the string.


    Next, whenever you make an assignment to a dynamic string, it's memory address is likely to change. Therefore, any preexisting STRPTR/STRPTR32 reference will likely become invalid. What this means is that if you change the string, you must "refresh" the string data address held by your byte pointers, etc.

    For example:
    Code:
    dim straptr as string ptr
    dim intaptr as byte ptr
    dim five as string
    
    cls
    straptr = varptr32(five)
    @straptr = "5"
    ' Above, we assign to the dereferenced string, 
    ' but now the string data has changed, so we must get the data
    ' address before we can access the string data with a pointer
    intaptr = strptr32(five) ' the data address
    print five, chr$(@intaptr)
    I hope this helps!

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

    Leave a comment:


  • Walt Decker
    started a topic Pointers vs DEF SEG

    Pointers vs DEF SEG

    The following produces different results:

    defint a - z
    cls
    dim straptr as string ptr
    dim intaptr as byte ptr
    dim five as string

    '********* These have the same address ************

    intaptr = varptr32(five)
    straptr = varptr32(five)

    ?intaptr
    ?straptr
    @straptr = "5"

    [email protected] '***** this produces "5"
    [email protected] '***** this produces 1

    A = strseg(five)
    b = strptr(five)
    def seg = a
    c = peek(b)
    J$ = peek$(b,1)
    def seg

    ?J$ '**** this produces "5"
    ?c, hex$(c) '**** this produces 53 and 35 (ascii codes)
    def seg
    end

    The questions is, which is correct the pointers or the peeks?
    I would expect [email protected] to produce 5 and [email protected] to produce 53 the same as peek does.

    ------------------
Working...
X