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

Just starting out? Need help? Post your questions and find answers here.
User avatar
Didelphodon
PureBasic Expert
PureBasic Expert
Posts: 450
Joined: Sat Dec 18, 2004 11:56 am
Location: Vienna - Austria
Contact:

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

Post 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.
Go, tell it on the mountains.
User_Russian
Addict
Addict
Posts: 1517
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

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

Post 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
User avatar
Didelphodon
PureBasic Expert
PureBasic Expert
Posts: 450
Joined: Sat Dec 18, 2004 11:56 am
Location: Vienna - Austria
Contact:

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

Post 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.
Go, tell it on the mountains.
juergenkulow
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

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

Post by juergenkulow »

PureBasic 2.90:
Image
User avatar
mk-soft
Always Here
Always Here
Posts: 6202
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

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

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply