我正在编写一个玩具QBasic编译器和一些测试。当我尝试创建一个向量类型时,我遇到了与REDIM
不一致的问题。
下面的代码可以在QBasic和QuickBasic 4.5解释器中工作。但是,当将第二个REDIM
编译为EXE时,它会产生'下标超出范围'错误。
DECLARE SUB RedimIntArray (arr() AS INTEGER)
DECLARE SUB RedimLongArray (arr() AS LONG)
' $DYNAMIC
DIM xs(2) AS INTEGER
PRINT UBOUND(xs)
RedimIntArray xs()
PRINT UBOUND(xs)
' $DYNAMIC
DIM ys(2) AS LONG
PRINT UBOUND(ys)
RedimLongArray ys()
PRINT UBOUND(ys)
SUB RedimIntArray (arr() AS INTEGER)
REDIM arr(10) AS INTEGER
END SUB
SUB RedimLongArray (arr() AS LONG)
REDIM arr(10) AS LONG
END SUB
这是预期的和记录的地方,如果是这样,有什么可能的修复?
乌利希期刊指南:上面的程序在QBX 7.1和QB64上运行良好,所以它可能与qb4.5编译器有关。
这确实是编译器的一个bug。
这篇知识库文章描述了这个问题:Q50638: "下标超出范围"If REDIM Long Integer Array in SUB.
REDIMing (redimensioning with REDIM) a dynamic long integer array that
was passed to a SUBprogram generates a "Subscript Out Of Range" error
at run time.
Microsoft has confirmed this to be a problem in Microsoft QuickBASIC
Versions 4.00, 4.00b, and 4.50 for MS-DOS and in Microsoft BASIC
Compiler Versions 6.00 and 6.00b for MS-DOS and MS OS/2 (buglist6.00,
buglist6.00b). This problem was corrected in Microsoft BASIC PDS
Version 7.00 (fixlist7.00).
The "Subscript Out Of Range" occurs whether the SUBprogram is compiled
as part of the main program or it is compiled in a separate module.
You can work around this problem by using an array type other than
long integer, or by passing the array through a COMMON SHARED block.