Array index question

This forum is meant for questions and discussions about the X# language and tools
Post Reply
boonnam
Posts: 88
Joined: Mon May 08, 2017 6:42 pm
Location: USA

Array index question

Post by boonnam »

I am testing my converted code from VO. I am using VO dialect with "Use Zero Based Arrays" set to false.

Here is a visual representation of aIntRate array (from VO debugger):
[1]
[1]=01/01/2017
[2]=3.0000
[3]=3.0000
[2]
[1]=09/29/2017
[2]=3.0000
[3]=3.0000

Here is the line that crash with index out of range error in X#:
dIntEff := aIntRate[ Min(n + 1, nMax), 1 ]

When this code run, on the first loop, the value for n is 1. The value for aMax is 2. So in this case, I am looking for the aIntRate[2,1] value, which is 09/29/2017. This is the result in VO.

When I run this same code against the same data, I get index out range error in X#. But if I decrement both n and nMax before this line run, I get the same result as in VO. Now my X# code look like this:
n--
nMax--
dIntEff := aIntRate[ Min(n + 1, nMax), 1 ]



This tell me the index under X# is zero base despite the setting. We have lot of arrays in our application. Any idea how to make sure the index is 1 base?

Thanks,
Boonnam
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Array index question

Post by FFF »

Boonam,
there has to be more...
Made a new X#/Vo app, NO compiler switch set:

Code: Select all

FUNCTION Start( ) AS VOID
	VAR x := {}
	AAdd(x, {1,2.3} )
	AAdd(x, {4,5,6})

//		? x[0,1]
VAR	 n:=1
VAR nMax:=2
? Min (n+1, nmax)

? x[ Min(n + 1, nMax), 1 ]	
? x[2,1]	
RETURN
This works.
Uncommenting the access to item 0 the app crashs at runtime.

Did you add something like above in your code to crosscheck?
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Array index question

Post by wriedmann »

Hi Boonnam,

I suspect there is something wrong with the compiler switches.

I have moved a lot of VO code, and have no problem with it (regarding arrays), and one of them is used daily.

You could output the following compiler macro:

Code: Select all

__ARRAYBASE__
to see how your code is compiled.

Wolfgang

P.S. this is a sample how to write code independently from the /az compiler switch, using the __ARRAYBASE__ compiler macro:

Code: Select all

local aStrings as string[]
local nI as int
local nLen as int
	
System.Console.WriteLine("Code independent from /az compiler setting")
if __ARRAYBASE__ == 0
  System.Console.WriteLine("0-based arrays")
else
  System.Console.WriteLine("1-based arrays")
endif
	
aStrings := <string> { "element 1", "element 2", "element 3", "element 4", "element 5" } 
// foreach works independently from the /az setting
foreach cString as string in aStrings
  System.Console.WriteLine( String.Format( "member is {0}", cString ) )
next	
	
nLen := aStrings:Length - 1 + __ARRAYBASE__
for nI := ( __ARRAYBASE__ ) upto nLen
  System.Console.WriteLine( String.Format( "member {0} is {1}", nI:ToString(), aStrings[nI] ) )
next
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
robert
Posts: 4518
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Array index question

Post by robert »

Boonnam,

Can you show us a little bit more of your code:

- What is the datatype of the variable where the array is stored (aIntRate)?
- What is the datatype of the array indexer (n)

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
Chris
Posts: 4898
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Array index question

Post by Chris »

Boonam,

First of course, please make sure you are using the latest build 1.0.3, because in 1.0.2 there was indeed a bug relates to arrays in specific cases.

If you are already using that, then to sum up the suggestions everybody made to you already, please set your app to type console application and surround the code like that to give some info when the error happens:

TRY
dIntEff := aIntRate[ Min(n + 1, nMax), 1 ]
CATCH e AS Exception
? "Execption caught"
? "__ARRAYBASE__ =", __ARRAYBASE__
? "n =" , n
? "nMax =" , nMax
? "Min(n + 1, nMax) =", Min(n + 1, nMax)
? "ALen(aIntRate) =", ALen(aIntRate)
? "ALen(aIntRate[1]) =", ALen(aIntRate[1])
? "ALen(aIntRate[2]) =", ALen(aIntRate[2])
END TRY

what are the values that are being printed?

Chris
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
boonnam
Posts: 88
Joined: Mon May 08, 2017 6:42 pm
Location: USA

Array index question

Post by boonnam »

Thank you everyone for the reply. I knew I didn't have this issue with version 1.01. I was using version 1.02. Version 1.03 does fix this issue.
Post Reply