address of string literal, concatenated string

Just starting out? Need help? Post your questions and find answers here.
HanPBF
Enthusiast
Enthusiast
Posts: 570
Joined: Fri Feb 19, 2010 3:42 am

address of string literal, concatenated string

Post 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!
Last edited by HanPBF on Wed Sep 09, 2015 1:16 pm, edited 1 time in total.
NEW NicknameFJ
New User
New User
Posts: 6
Joined: Fri Sep 04, 2015 8:32 pm

Re: address of stliteral, concatenated string

Post 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
ElementE
Enthusiast
Enthusiast
Posts: 139
Joined: Sun Feb 22, 2015 2:33 am

Re: address of stliteral, concatenated string

Post 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
Last edited by ElementE on Wed Sep 09, 2015 11:44 am, edited 1 time in total.
Think Unicode!
DontTalkToMe
Enthusiast
Enthusiast
Posts: 334
Joined: Mon Feb 04, 2013 5:28 pm

Re: address of stliteral, concatenated string

Post by DontTalkToMe »

I hope he knows that, he was asking how to get the address of the temporary string resulting from the concatenation.
ElementE
Enthusiast
Enthusiast
Posts: 139
Joined: Sun Feb 22, 2015 2:33 am

Re: address of stliteral, concatenated string

Post 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?
Think Unicode!
ElementE
Enthusiast
Enthusiast
Posts: 139
Joined: Sun Feb 22, 2015 2:33 am

Re: address of stliteral, concatenated string

Post 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!
Think Unicode!
User avatar
Demivec
Addict
Addict
Posts: 4282
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: address of stliteral, concatenated string

Post 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)
ElementE
Enthusiast
Enthusiast
Posts: 139
Joined: Sun Feb 22, 2015 2:33 am

Re: address of stliteral, concatenated string

Post 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.
Think Unicode!
HanPBF
Enthusiast
Enthusiast
Posts: 570
Joined: Fri Feb 19, 2010 3:42 am

address of string literal, concatenated string

Post 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!
User avatar
kenmo
Addict
Addict
Posts: 2056
Joined: Tue Dec 23, 2003 3:54 am

Re: address of stliteral, concatenated string

Post 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!"
HanPBF
Enthusiast
Enthusiast
Posts: 570
Joined: Fri Feb 19, 2010 3:42 am

Re: address of string literal, concatenated string

Post 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!
Post Reply