Page 1 of 1

PB6.03 LTS (x64,ASM) - @-operator on implicitly declared empty string leads to address 0

Posted: Wed Nov 22, 2023 11:57 am
by Didelphodon
As far as I know, Purebasic declares and defines variables even if they are not reached directly through conditional code during runtime. The following code demonstrates a situation where this is not the case anymore, leading to a 0-based string variable:

Code: Select all

cycles.i = 0
For i.i = 1 To cycles
	s.s + "*"
Next i
Debug @s
Note: The according debug output is "0"!

As a workaround, the following modification of the above code leads to a legit address:

Code: Select all

s.s = ""
cycles.i = 0
For i.i = 1 To cycles
	s.s + "*"
Next i
Debug @s
That new behaviour triggers crashes in my projects as I was relying on the original behaviour, because as far as I know, the original behaviour was on purpose.

Re: PB6.03 LTS (x64,ASM) - @-operator on implicitly declared empty string leads to address 0

Posted: Wed Nov 22, 2023 12:34 pm
by User_Russian
This is not a bug.
If the string is empty, there is no need to assign a memory address to it.

Code: Select all

s.s
Debug @s
s="1234"
Debug @s
s = #Null$
Debug @s

Re: PB6.03 LTS (x64,ASM) - @-operator on implicitly declared empty string leads to address 0

Posted: Wed Nov 22, 2023 12:44 pm
by Didelphodon
That depends on how a programming language is designed. And Purebasic so far treated empty strings as actual strings (which would also be my approach). In terms of generally discussing the need for an actual address under such circumstances and memory optimization goals you're right. In terms of supporting direct-address-access in general, that should not be the case as it's just not consistent behaviour and cumbersome/annoying to conditinally deal with in coding as an empty string is still a valid string of an actual existing string variable.

UPDATE:
And how would you provide a string variable by reference to a procedure which is designed to populate that string variable directly, so not using return-values? In my case I'm communicating with scintilla-gadgets with the need to provide an empty string sometimes. I can work around it, I'm totally aware of that, but the former behaviour was different. However, maybe I relied on something that wasn't implemented on purpose.

[No Bug] Re: PB6.03 LTS (x64,ASM) - @-operator on implicitly declared empty string leads to address 0

Posted: Wed Nov 22, 2023 1:22 pm
by juergenkulow
PureBasic 2.90:
Image

Re: PB6.03 LTS (x64,ASM) - @-operator on implicitly declared empty string leads to address 0

Posted: Wed Nov 22, 2023 6:36 pm
by mk-soft
A Null$ or an unused string is not an Empty$ string
A Null$ means that there is no memory for the string yet and an Empty$ is a pointer to a null terminated empty string

String By Ref ...
To pass a string ByRef, you need a pointer to the variable where the pointer is stored on the string

Code: Select all

;-TOP

Procedure MyFunction(*pString.String)
  *pString\s = "Hello World"
EndProcedure

; Variable where stored the pointer to String
Define pString.string

Debug "Adress to Variable: " + @pString
Debug "Adress to String: " + @pString\s
Debug "String: " + pString\s

MyFunction(@pString)

Debug "Adress to Variable: " + @pString
Debug "Adress to String: " + @pString\s
Debug "String: " + pString\s