Structured strings and plain strings
Posted: Thu Jun 22, 2017 2:37 pm
Sometimes when passing strings around, or mixing structured strings with normal strings, or using pointers, etc. the relationship between these different string representations may be confusing.
Maybe this can be of some help to someone.
Maybe this can be of some help to someone.
Code: Select all
; a pb structured string is a structure like this
;
; Structure String
; s.s
; EndStructure
; when you modify the s.s string, its address @s may change, because the string contents are reallocated somewhere else if required
; the base address of the structure never change
; a normal unstructured a$ string it's actually stored inside a structure too but that structure is not visible
; again the address of this string may change when modified
; the base address of the "invisible" structure never change
; some pratical examples to explore these relationships (better go through them using the step debugger)
#SPACES = 64 ; this is to try to force a reallocation when updating the string, use a bigger number if required
; when using a structured string
Define a.string\s = "hello"
Debug @a ; base address of the PB string structure, the string is NOT in there, the structure contains just a pointer to the string
Debug @a\s ; this is the "PB way" to get the address of actual string
Debug PeekI(@a) ; this is equivalent to the above, you read the pointer located at the start of the structure
Debug a\s ; this is the "PB way" to print the string
Debug PeekS(@a\s) ; this is equivalent to the above
Debug PeekS(PeekI(@a)) ; and this too is equivalent to the above
a\s = a\s + Space(#SPACES) + "world" ; if you modify the string ...
Debug a\s ; string is changed
Debug @a\s ; ... you'll see the address of the string is probably changed, because a new allocated string replaced the old one
Debug @a ; ... while the address of the structured string is still the same
Debug "--------------------"
; when using a plain string
; the string it's still part of a structured PB string, but the structure it's not directly accessible
Define a$ = "hello"
Debug @a$ ; this is the address of the actual string
Debug a$ ; this prints the string
Debug PeekS(@a$) ; so this is equivalent to the above
a$ = a$ + Space(#SPACES) + "world" ; if you modify the string ...
Debug a$ ; string is changed
Debug @a$ ; ... you'll see the address of the string is probably changed, because a new allocated string replaced the old one
Debug PeekS(@a$) ; so this is equivalent to the above
; the base address of the structured string is not changed, but you can't access it anyway
; ... unless you use some asm
Define *p
CompilerIf (#PB_Compiler_Processor = #PB_Processor_x86)
! lea ecx,[v_a$] ; this load the address of where the PB structured string containing a$ is in memory
! mov [p_p], ecx ; and copies it to *p
CompilerElse
! lea rcx,[v_a$] ; this load the address of where the PB structured string containing a$ is in memory
! mov [p_p], rcx ; and copies it to *p
CompilerEndIf
Debug *p ; base address of the "invisible" PB string structure
Debug PeekI(*p) ; this is the pointer located at the start of the structure which points to a$
Debug PeekS(PeekI(*p)) ; and a$ contents are there