Page 1 of 1

address of string literal, concatenated string

Posted: Wed Sep 09, 2015 11:18 am
by HanPBF
Hello,

how can I get the address of a string concatenation (after concatenation)?

Code: Select all

@"  Hello, " + "world!"; error; type mismatch
@("Hello, " + "world!"); error; syntax error
Can it be solved with a constant string literal?

Thanks a lot!

Re: address of stliteral, concatenated string

Posted: Wed Sep 09, 2015 11:28 am
by NEW NicknameFJ
Hello,

Code: Select all

"  Hello, " + "world!"
isn`t a literal string anymore.

Use

Code: Select all

Dummy$ = "  Hello, " + "world!"
debug @Dummy$
NicknameFJ

Re: address of stliteral, concatenated string

Posted: Wed Sep 09, 2015 11:38 am
by ElementE
To get the address of the string data, you have to use a string variable.
The @ operator applied to the string variable will give the address of the string data in memory.

Here is some example code.

Code: Select all

; A PureBasic string variable is a memory address pointer to the actual string data in memory

; The string Manager assigns memory to string variable's data
Text$="Hello, "
Debug Text$
Debug @Text$

; The string manager has allocated some "extra memory" for the string data,
; so adding a small number of characters to the string variable does
; not change the string data's memory address.
Text$= "Hello, "+"World"
Debug Text$
Debug @Text$

; Adding more characters will result in the
; string Manager allocating more memory and moving the string data to another address
Text$ = Text$+ ", ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Debug Text$
Debug @Text$
Running the above using PB 5.31 on a 32-bit Vista computer gave the following Debug output:

Hello,
2426848
Hello, World
2426848
Hello, World, ABCDEFGHIJKLMNOPQRSTUVWXYZ
2443328

Re: address of stliteral, concatenated string

Posted: Wed Sep 09, 2015 11:42 am
by DontTalkToMe
I hope he knows that, he was asking how to get the address of the temporary string resulting from the concatenation.

Re: address of stliteral, concatenated string

Posted: Wed Sep 09, 2015 11:57 am
by ElementE
The key is that the concatenation has to be assigned to a string variable in order to get the address of the string data.

This leads to the question, what happens to old string data?
Does the PureBasic string management free up string memory (wherever that might be, the heap?) by detecting and deleting old string data that is no longer assigned to a string variable?

Re: address of stliteral, concatenated string

Posted: Wed Sep 09, 2015 12:10 pm
by ElementE
With regards to the specific original question.

It appears that the @ operator can only be applied to string variables, not to string expressions!

Re: address of stliteral, concatenated string

Posted: Wed Sep 09, 2015 12:26 pm
by Demivec
ElementE wrote:With regards to the specific original question.

It appears that the @ operator can only be applied to string variables, not to string expressions!
As HanPBF pointed out, '@' can also be applied to string literals.


@HanPBF: you can't solve this with a constant because the '@' operator can't be used with a constant either.

You can however accomplish something similar by using a DataSection, using only concatenated string literals.

Code: Select all

DataSection
  testString:
  Data.s "Hello, " + "world!"
EndDataSection

Debug ?testString
Debug PeekS(?testString)

Re: address of stliteral, concatenated string

Posted: Wed Sep 09, 2015 1:13 pm
by ElementE
It seems that @ does not like to operate on anything inside parenthesis.

Both

Code: Select all

text$="Hello"
A.l=@(text$)
and

Code: Select all

A.l=@("Hello")
give syntax errors.

address of string literal, concatenated string

Posted: Wed Sep 09, 2015 1:21 pm
by HanPBF
Thanks a lot everybody for the clarification!

So, as

Code: Select all

@("Hello, " + "world!")
has no meaning as an address of an expression (cause we don't have anonymous procedures yet :wink: ),
it should be handled by the compiler or a precompiler.

I will work with variables instead.

Thanks!

Re: address of stliteral, concatenated string

Posted: Wed Sep 09, 2015 1:39 pm
by kenmo
Fun with string pointers.

Code: Select all

Debug PeekS(@"Literal") ; Valid syntax
;Debug PeekS(@("Literal1" + " Literal2")) ; Invalid syntax - not concatenated at compile-time
Debug ""

StringVar.s = "Var1" + " Var2"
Debug PeekS(@StringVar)
Debug ""

Debug  "Hello"  +  " World!"  ; Normal
Debug ("Hello"  +  " World!") ; All in parentheses
Debug ("Hello") +  " World!"  ; Bug???
;Debug  "Hello"  + (" World!") ; Invalid syntax
Debug ""

DataSection
  StringData1:
  Data.s "Data"
  StringData2:
  Data.s "Data1" + " Data2"
  StringPtr:
  Data.i @"DataPtr"
EndDataSection

Debug PeekS(?StringData1)
Debug PeekS(?StringData2) ; Valid syntax - concatenated at compile-time
Debug PeekS(PeekI(?StringPtr))
This line seems like a PB bug:

Code: Select all

Debug ("Hello") +  " World!"

Re: address of string literal, concatenated string

Posted: Mon Jul 18, 2016 10:44 am
by HanPBF
This works now in 5.50:

Code: Select all

debug @"Hello, world!"
This still not works:

Code: Select all

debug @("Hello, " + "world!")

Is parser implementation not strictly done via AST?
Or does a syntax check before blocks this?

Thanks!