Q: String Internals [Answered]

General FreeBASIC programming questions.
rdc
Posts: 1725
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Q: String Internals [Answered]

Postby rdc » Apr 11, 2006 0:46

I'd like to get some clarification on the string internals from the gurus:

I have the following program:

Code: Select all

Option Explicit

'declare some string variables
Dim myDynaString as String
Dim myFixedString as String * 20
Dim myZString as ZString * 30
Dim myWString as WString * 30

'add some data
myDynaString = "Hello World From FreeBasic!"
myFixedString = "Hello World!"
myZString = "Hello World From FreeBasic!"
myWString = "Hello World From FreeBasic!"

'print out the lengths of strings
Print "Length of Dynamic String is";Len(myDynaString);" byte(s)."
Print "Length of Fixed String";Len(myFixedString);" byte(s)."
Print "Length of ZString is";Len(myZString);" byte(s)."
Print "Length of WString is";Len(myWString);" byte(s)."
Print

'print out the variable size
Print "Size of Dynamic String is";SizeOf(myDynaString);" byte(s)."
Print "Size of Fixed String";SizeOf(myFixedString);" byte(s)."
Print "Size of ZString is";SizeOf(myZString);" byte(s)."
Print "Size of WString is";SizeOf(myWString);" byte(s)."

'wait for keypress
Sleep
End


Which produces the output:

Code: Select all

Length of Dynamic String is 27 byte(s).
Length of Fixed String 20 byte(s).
Length of ZString is 27 byte(s).
Length of WString is 27 byte(s).

Size of Dynamic String is 12 byte(s).
Size of Fixed String 21 byte(s).
Size of ZString is 30 byte(s).
Size of WString is 60 byte(s).


So, is the following correct? If not, what are the details?

1) Len returns the length of the string data, sizeof returns the size of the variable.
2) The size (using sizeof) for the dynamic string (12) is the length of the string descriptor.
3) The size for the fixed string (21) includes the terminating null byte.
4) If you explicitly declare a size for the Z and WString you don't need to add the terminating null when assigning a value.

As an aside, what is contained in the string descriptor for dynamic strings? I don't want to mess with it, just provide a general idea of the descriptor for the book I am working on.

Thanks!
Last edited by rdc on Apr 12, 2006 13:03, edited 1 time in total.
Fragmeister
Posts: 545
Joined: Nov 08, 2005 14:36

Postby Fragmeister » Apr 11, 2006 2:21

Looks right to me. LEN returns the length of a string, or the size in bytes of a data type. SIZEOF just returns the size in bytes of a datatype.
The string descriptor is a TYPE that contains an INTEGER with the string length, and a pointer to the string data (a UBYTE ptr, probably.) I'm not sure on this, but I'm pretty sure. If you happen to have the compiler source handy, it'sprobably in one of the include files.
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Apr 11, 2006 2:46

Code: Select all

Type stringstruct

  dat As zString Ptr
  length As Integer
  size As Integer
 
End Type



Declare Sub strdata( s As String, i As Integer = 0 )



Dim As String a, b

a = "hehehe"
b = "2 string are better than one"

strdata( a )
strdata( b )

Sleep





Sub strdata( s As String, i As Integer = 0 )

  Dim As stringstruct Ptr fbstring = cptr( stringstruct Ptr, @s )
 
  ? *( fbstring->dat )
  ? fbstring->length
 
End Sub
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Apr 11, 2006 2:49

size contains the amount of memory internally allocated for the string. the string memory is allocated in big chunks, or pools, so sometimes there will be more memory allocated for a string than needed, the up side being that's much faster to do.
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

Postby voodooattack » Apr 11, 2006 8:31

really nice work there cha0s :)
rdc
Posts: 1725
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Postby rdc » Apr 11, 2006 13:02

Good info cha0s. thanks.
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Apr 11, 2006 22:59

no problem :)
erosolmi
Posts: 16
Joined: May 12, 2007 15:03
Location: Milan - Italy
Contact:

Postby erosolmi » Sep 20, 2007 12:20

Although this post is not recent, thanks a lot for this info. Exactly what I was searching for.

Eros

Return to “General”

Who is online

Users browsing this forum: Bing [Bot] and 5 guests