Why wrong length of array

New to FreeBASIC? Post your questions here.
Post Reply
rnbas
Posts: 35
Joined: Jul 22, 2019 18:54

Why wrong length of array

Post by rnbas »

I have just started using Freebasic and it is very easy to use.

I am trying following function from: viewtopic.php?t=13975 :

Code: Select all

Public Sub Split(Text As String, Delim As String = " ", Count As Long = -1, Ret() As String)
   Dim As Long x, p
   If Count < 1 Then
      Do
         x = InStr(x + 1, Text, Delim)
         p += 1
      Loop Until x = 0
      Count = p - 1
   ElseIf Count = 1 Then
      ReDim Ret(Count)
      Ret(0) = Text
   Else
      Count -= 1
   End If
   Dim RetVal(Count) As Long
   x = 0
   p = 0
   Do Until p = Count
      x = InStr(x + 1,Text,Delim)
      RetVal(p) = x
      p += 1
   Loop
   ReDim Ret(Count)
   Ret(0) = Left(Text, RetVal(0) - 1 )
   p = 1
   Do Until p = Count
      Ret(p) = Mid(Text, RetVal(p - 1) + 1, RetVal(p) - RetVal(p - 1) )
      p += 1
   Loop
   Ret(Count) = Mid(Text, RetVal(Count - 1) + 1)
End Sub
On trying it out:

Code: Select all

Dim As Long MaxCount
Dim As String s() , text , Delimiter

text = "enter the text to split"
MaxCount = -1
' if 2, second part will be whole text from second word onwards; 
Delimiter = " "

split text, Delimiter, MaxCount, S() 

print "length of s is: "; len(S)
for i as integer =0 to (len(s)-1)
	print i; ": "; s(i)
next
I get following output:

Code: Select all

length of s is:  12
 0: enter
 1: the 
 2: text 
 3: to 
 4: split
 5: ./rnfbc: line 2: 11167 Segmentation fault      ./$1
Why the length of S() is coming as 12? I expected it to be 5.
Thanks for your help.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Why wrong length of array

Post by D.J.Peters »

you don't need len(array) use ubound(array) instead.

Len() VS. UBound()

Joshy

Code: Select all

Public Sub Split(Text As String, Delim As String = " ", Count As Long = -1, Ret() As String)
   Dim As Long x, p
   If Count < 1 Then
      Do
         x = InStr(x + 1, Text, Delim)
         p += 1
      Loop Until x = 0
      Count = p - 1
   ElseIf Count = 1 Then
      ReDim Ret(Count)
      Ret(0) = Text
   Else
      Count -= 1
   End If
   Dim RetVal(Count) As Long
   x = 0
   p = 0
   Do Until p = Count
      x = InStr(x + 1,Text,Delim)
      RetVal(p) = x
      p += 1
   Loop
   ReDim Ret(Count)
   Ret(0) = Left(Text, RetVal(0) - 1 )
   p = 1
   Do Until p = Count
      Ret(p) = Mid(Text, RetVal(p - 1) + 1, RetVal(p) - RetVal(p - 1) )
      p += 1
   Loop
   Ret(Count) = Mid(Text, RetVal(Count - 1) + 1)
End Sub

dim As Long MaxCount
Dim As String s() , text , Delimiter

text = "enter the text to split"
MaxCount = -1
' if 2, second part will be whole text from second word onwards;
Delimiter = " "

split text, Delimiter, MaxCount, S()

print "length of s is: "; ubound(S)
for i as integer =0 to ubound(S)
   print i; ": "; s(i)
next
sleep
SARG
Posts: 1767
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Why wrong length of array

Post by SARG »

If "length of s is:" should show the number of words you need to add one to the ubound as it's zero based..

Code: Select all

print "length of s is: "; ubound(S)+1
rnbas
Posts: 35
Joined: Jul 22, 2019 18:54

Re: Why wrong length of array

Post by rnbas »

Yes, ubound works well. Thanks.

What does len(S) show? Why it is 12?
fxm
Moderator
Posts: 12131
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Why wrong length of array

Post by fxm »

For an array 'S()', 'S' is not an element but the symbol-name of the array.
In that case, 'Len(S)' is interpreted as 'Len(Typeof(S))'.
If 'S()' is a String array, 'Len(String)' returns the length in Byte of the string descriptor (the length of 3 Integers).
Lost Zergling
Posts: 538
Joined: Dec 02, 2011 22:51
Location: France

Re: Why wrong length of array

Post by Lost Zergling »

@rnbas. FB is a language that is much more powerful than its ease of handling suggests at first glance. As you later wish to go further, pointers and object-oriented syntax are available. For example, the Mid function repeated on the same string can be replaced by the use of pointers (4x faster especially because the memory management is shared by the algorithm instead of being repeated in each Mid). It is same for memory management functions (Dim, Redim, Preserve) which are in their own way "macro-commands" compared to a fine and customized strategy of memory management (can be 10x faster and more). As a first step, you can identify the possibilities to use the Redim and Preserve functions as less as possible (for example by defining or identify a maximum number of elements in the same array that you systematically reuse and virtually managing the logical number of elements), especially in the repetitive code (split repeated a lot of times).
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Why wrong length of array

Post by MrSwiss »

fxm wrote:If 'S()' is a String array, 'Len(String)' returns the length in Byte of the string descriptor (the length of 3 Integers).
While the statement above is true, be careful with "Integer", because it isn't a "fixed size" data-type.

I can tell straight away, by the 12 Bytes, that you've used FBC 32 bit, to compile.
With FBC 64 bit, the size whould be: 24 Bytes (still: 3 x Integer).
rnbas
Posts: 35
Joined: Jul 22, 2019 18:54

Re: Why wrong length of array

Post by rnbas »

I have a question regarding 32bit versus 64bit version.
I am using 32bit fbc on 64bit Debian Stable Linux (after installing required libraries) and it is working very well.
Will programs that I compile run both on 32bit and 64bit systems?
What is the best way to ensure that compiled programs work on both 32bit and 64bit systems?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Why wrong length of array

Post by MrSwiss »

rnbas wrote:Will programs that I compile run both on 32bit and 64bit systems?
What is the best way to ensure that compiled programs work on both 32bit and 64bit systems?
FBC 32 bit, should do the trick provided, that the 64 bit system has a 32 bit emulator built-in.
(This may change, in the future ... ?!? Who knows?)

Personally I'm compiling everything new, for 64 bit only (it's become mainstream, by now).
Post Reply