Microsoft KB Archive/66455

= Problems May Occur When Passing the Same Array Element Twice =

Article ID: 66455

Article Last Modified on 11/21/2006



This article was previously published under Q66455



SUMMARY
The following program may give unexpected results when the same array element is passed twice to the subprogram. The problem results from a form of variable aliasing, where the same memory location is referenced by two different variables.

To avoid aliasing problems, never pass the same variable twice in a given parameter list.



MORE INFORMATION
Passing the same array element twice in the same parameter list can give incorrect or unexpected results regardless of array type or dynamic or static array allocation. The results may also vary between compiler versions. A customer reported that the program below gave the results that he wanted in QuickBasic 4.00, but not in Microsoft Basic Professional Development System (PDS) version 7.10; Microsoft has not confirmed this report.

This behavior results from the fact the Basic often requires a far pointer to access arrays, but parameters need to be passed as near pointers. On a CALL, Basic sets aside a temporary location holding the array element and then passes a pointer to the temporary area.

There are two options in this sort of situation: Recode the subprogram so that it is not necessary to pass the array element twice, or assign one of the parameters to a temporary variable and then pass the temporary variable.



Code Example
DECLARE SUB MakeUpper(instring AS STRING, outstring AS STRING) DIM a$(15) a$(4)=&quot;abcdefg&quot; CALL MakeUpper(a$(4), a$(4)) PRINT a$(4) END

SUB MakeUpper(instring AS STRING, outstring AS STRING) outstring = UCASE$(instring) END SUB

Additional query words: QuickBas BasicCom

Keywords: KB66455

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.