String memory

New to FreeBASIC? Post your questions here.
Post Reply
sonhn
Posts: 2
Joined: Mar 05, 2019 1:20

String memory

Post by sonhn »

Hello,
I wonder why code 1 compiled normally while code 2 not:
Code1:

Code: Select all

dim s as string = environ("PATH")
dim z as ZString ptr = strptr(s)
Code2:

Code: Select all

dim z as ZString ptr = strptr(environ("PATH"))
With Code2, the compiler output error24: Invalid data types, for STRPTR in 'dim z as ZString ptr = strptr(environ("PATH"))'
Thank you.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: String memory

Post by MrSwiss »

Hi sonhn, welcome ...

Code 1:
Menory is allocated in String (data-type), therefore correct.
Code 2:
ZString Ptr, can only store a strings address, not the string itself, which is, what you are attempting.
(except: if you (c)allocate enough memory, to hold the sring's data!)

working method 2:

Code: Select all

dim as zstring ptr  psz = CAllocate(1024, 1)	' allocate memory

*psz = Environ("path")  ' dereference the ptr (store string data)
Print *psz              ' show it

Sleep
' clean up: free alloc'ed memory
DeAllocate(psz)
PS: this is the "in a nutshell" version ...

See: FB-Manual (aka: Documentation) for more details on such things.
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: String memory

Post by fxm »

'Environ()' is a function which returns a 'String' by value.
So its result is not a reference but a temporary variable which will be deleted at the end of the line execution calling it.
Therefore the compiler disallows the access to its descriptor and data addresses (syntaxes such as '@Environ(varname)' and 'Strptr(Environ(varname))' are prohibited).

You can only copy its value into a user variable (a static/dynamic String/Zstring for example):

Code: Select all

'Using a static String
Dim As String s
s = Environ("path")
Print s
Print

'Using a static ZString pre-sized
Dim As ZString * 1024 z
z = Environ("path")
Print z
Print

'Using a dynamic String
Dim As String Ptr ps = New String
*ps = Environ("path")
Print *ps
Print
Delete ps

'Using a dynamic ZString pre-sized (as proposed by MrSwiss)
Dim As ZString Ptr pz = CAllocate(1024, Sizeof(UByte))
*pz = Environ("path")
Print *pz
Print
DeAllocate(pz)

Sleep
sonhn
Posts: 2
Joined: Mar 05, 2019 1:20

Re: String memory

Post by sonhn »

Thank you for yours answers,

So because the temporary variable will be delete, the compiler prevent referencing it. As I understand, memory must be allocated 2 times: (1) when then temp variable is created; (2) create another string to copy that variable to ?
fxm
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: String memory

Post by fxm »

Yes.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: String memory

Post by MrSwiss »

As I understand, memory must be allocated 2 times: (1) when then temp variable is created; (2) create another string to copy that variable to ?
(1) is handled by the Compiler (internally), you don't have "to do" anything, but:
(2) is mandatory, in your own code (aka: your responsibility)

Just to clarify the difference ...
Post Reply