Q: String Internals [Answered]

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

Q: String Internals [Answered]

Post by rdc »

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

Post by Fragmeister »

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: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

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: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

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:

Post by voodooattack »

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

Post by rdc »

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

Post by cha0s »

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

Post by erosolmi »

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

Eros
Post Reply