About pointer related

New to FreeBASIC? Post your questions here.
Post Reply
PeterHu
Posts: 159
Joined: Jul 24, 2022 4:57

About pointer related

Post by PeterHu »

I am reading the manual trying to understand pointer,@,strptr,valptr and string related in a deep manner,but I encountered problems.
Below code mainly focused in two question I wanted help.
Question No.1--How to retrieve back the data with toStrDes(which is a pointer to an internal data descriptor points to ),I mean the data,here in this example the data I want to retrieve back is "This is a string",not the internal desciptor address?
Question No.2-- Why retrieving back the data that strptr pointer to in the middile command failed-by failed I mean the result shown in the console with interruption data not the accurate data(see comments in the related line of the source) ,whatever I changed the source encoding or even changed between fbc compilers from 1.09-1.10 to 1.20?

I am expecting an in-depth explanation.

Thanks for the help in advance.

Code: Select all


 Dim As String myStr="This is a string"
 Dim toStrDes As Any Ptr
 Dim toStr As ZString Ptr
 toStrDes=@myStr  '''pointer to internal data descriptor other than data itself
 ? "myStr=";"This is a string"  '''just to easy trace down and comparison
 ? "toStrDes:";toStrDes
 '''Question No.1--How to retrieve back the data (not the internal desciptor) that toStrDes pointer pointed to?
 '? *toStrDes
 ? "@myStr:";@myStr
 toStr=StrPtr(myStr)
 ? "toStr:";toStr
 ? "*toStr:";*toStr
 
 myStr="The string is changed to very very ---------------------------------------------------------------long."
 ? "myStr=";"The string is changed."
 ? "toStrDes:";toStrDes
 ? "@myStr:";@myStr
 ? "toStr:";toStr
 Print "*toStr:";*toStr   ''''Question No.2---data inerruptions when showing in the console
 GetKey()
 ? "Assign variable again--"
 toStrDes=@myStr
 toStr=StrPtr(myStr)
 ? "toStrDes:";toStrDes
 ? "@myStr:";@myStr
 ? "toStr:";toStr
 ? "*toStr:";*toStr


fxm
Moderator
Posts: 12132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: About pointer related

Post by fxm »

1)
To retrieve the string's character data from a pointer to the string descriptor, you must use a proper typed pointer (and not a 'Any Ptr'):
Dim toStrDes As String Ptr
.....
? "*toStrDes:";*toStrDes


2)
When a variable length string is modified, the address of its descriptor remains always the same, but the the string's character data address (returned by Operator 'Strptr') may change (like any allocated memory that must be reallocated).
So after each 'myStr' length change, you must refresh the 'toStr' pointer value before using it:
toStr=StrPtr(myStr)
Print "*toStr:";*toStr
PeterHu
Posts: 159
Joined: Jul 24, 2022 4:57

Re: About pointer related

Post by PeterHu »

fxm wrote: Nov 03, 2023 14:26 1)
To retrieve the string's character data from a pointer to the string descriptor, you must use a proper typed pointer (and not a 'Any Ptr'):
Dim toStrDes As String Ptr
.....
? "*toStrDes:";*toStrDes


2)
When a variable length string is modified, the address of its descriptor remains always the same, but the the string's character data address (returned by Operator 'Strptr') may change (like any allocated memory that must be reallocated).
So after each 'myStr' length change, you must refresh the 'toStr' pointer value before using it:

toStr=StrPtr(myStr)
Print "*toStr:";*toStr
Thanks a lot.
Just found *cptr(string ptr,toStrDes) gives me the result.So cptr(string ptr,toStrDes) works like (char*)a_void_pointer in C,am I right?
So after each 'myStr' length change, you must refresh the 'toStr' pointer value before using it:
So in practice this is quite different to C.Here I am afraid I cannot remember everytime refreshing the binding
fxm
Moderator
Posts: 12132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: About pointer related

Post by fxm »

The var-len string ('Dim As String ...') is a type specific to FreeBASIC (do not confuse with the fix-len strings like 'Dim As String * N ...', 'Dim As Zstring * N', ...).
A var-len string is a dynamic length string which is automatically resized if necessary, by using a dynamic memory for the string character data with an optimized management to avoid systematic memory reallocations.
It is why this object is referenced by an internal descriptor of 1 pointer + 2 uinteger:
- address of the string character data,
- used length of the string character data,
- available length allocated.
A such object must be preferentially used (or passed) through is descriptor (or a pointer to its descriptor), and not through a direct pointer to its string character data.

If you absolutely want to use a kind of pointer to the string character data, so use a pointer to the pointer in the descriptor to the string character data:

Code: Select all

Dim As String myStr = "This is a string"
Dim toToStr As ZString Ptr Ptr
toToStr = Cptr(Any Ptr, @myStr)
? "toToStr: "; toToStr
? "*toToStr: "; *toToStr
? "**toToStr: "; **toToStr
?
myStr = "The string is changed to very very ---------------------------------------------------------------long."
? "toToStr: "; toToStr
? "*toToStr: "; *toToStr
? "**toToStr: "; **toToStr
Sleep

If you use instead a reference to the pointer in the descriptor to the string character data, you can so avoid an explicit double de-referencing (the first de-referencing becomes implicit):

Code: Select all

Dim As String myStr = "This is a string"
Dim Byref As Zstring Ptr toStr = *Cptr(Zstring Ptr Ptr, 0)
@toStr = Cptr(Zstring Ptr Ptr, @myStr)
? "toStr: "; toStr
? "*toStr: "; *toStr
?
myStr = "The string is changed to very very ---------------------------------------------------------------long."
? "toStr: "; toStr
? "*toStr: "; *toStr
Sleep
PeterHu
Posts: 159
Joined: Jul 24, 2022 4:57

Re: About pointer related

Post by PeterHu »

fxm wrote: Nov 04, 2023 9:11 The var-len string ('Dim As String ...') is a type specific to FreeBASIC (do not confuse with the fix-len strings like 'Dim As String * N ...', 'Dim As Zstring * N', ...).
A var-len string is a dynamic length string which is automatically resized if necessary, by using a dynamic memory for the string character data with an optimized management to avoid systematic memory reallocations.
It is why this object is referenced by an internal descriptor of 1 pointer + 2 uinteger:
- address of the string character data,
- used length of the string character data,
- available length allocated.
A such object must be preferentially used (or passed) through is descriptor (or a pointer to its descriptor), and not through a direct pointer to its string character data.

If you absolutely want to use a kind of pointer to the string character data, so use a pointer to the pointer in the descriptor to the string character data:

Code: Select all

Dim As String myStr = "This is a string"
Dim toToStr As ZString Ptr Ptr
toToStr = Cptr(Any Ptr, @myStr)
? "toToStr: "; toToStr
? "*toToStr: "; *toToStr
? "**toToStr: "; **toToStr
?
myStr = "The string is changed to very very ---------------------------------------------------------------long."
? "toToStr: "; toToStr
? "*toToStr: "; *toToStr
? "**toToStr: "; **toToStr
Sleep

If you use instead a reference to the pointer in the descriptor to the string character data, you can so avoid a double de-referencing (the first de-referencing becomes implicit):

Code: Select all

Dim As String myStr = "This is a string"
Dim Byref As Zstring Ptr toStr = *Cptr(Zstring Ptr Ptr, 0)
@toStr = Cptr(Zstring Ptr Ptr, @myStr)
? "toStr: "; toStr
? "*toStr: "; *toStr
?
myStr = "The string is changed to very very ---------------------------------------------------------------long."
? "toStr: "; toStr
? "*toStr: "; *toStr
Sleep

These explanation are what I really really wanted ,by studying all the above information,hope I am stepping closer to finally fully understand all about pointer(esp. string related) in FB:ah~it is so simple!

The thing is from when I review one of my previous post here asking for help about win32 api,at the first glance,I think I've understood this subject,but after a second and third thought,I felt I was kinda of lost,to be specific,I was totally lost in strptr(filter), @file,@"Open",@someFile,*someFile.lpstrFile.So I realized that actually I did not understand pionter with strings at all in FB,and they are NOT exactly the same as in C.So re-read the manual and ask for help here.

Appreciated.

Code: Select all

'#define UNICODE
#include "windows.bi"
#include "win/commdlg.bi"

Function openFileDialog() As String
    Dim As zstring * 2048 FILE
    Dim As String filter="Text Files(.txt)"+chr(0)+"*.txt"+chr(0)+"All Files"+chr(0)+"*.*"+chr(0,0)
    Dim As OpenFileName SomeFile
    With SomeFile
        .lStructSize = Sizeof(OpenFileName)
        .hInstance = null
        .lpstrFilter = Strptr(filter)
        .lpstrFile = @FILE
        .nMaxFile = 2048
        .nMaxFileTitle = 0
        .lpstrTitle =@"Open"
        .Flags = OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST
        .hwndOwner=0
    End With
    if(GetOpenFileName(@SomeFile)) then
      MessageBox(null,SomeFile.lpstrFile,"You picked",0)
   else
      MessageBox(null,"Failed to open the file","Error",MB_OK or MB_ICONEXCLAMATION)
   end if
    Return *SomeFile.lpstrFile
End Function
print openFileDialog()
sleep
Post Reply