Microsoft KB Archive/37305

From BetaArchive Wiki
Knowledge Base


LONG Integer Parameter Passed to SUB Fails after Assignment

Article ID: 37305

Article Last Modified on 11/21/2006

This article was previously published under Q37305

SYMPTOMS

A problem can occur in an .EXE program when you pass a LONG integer parameter to a subprogram and then assign that parameter to a local short integer. The long integer parameter becomes corrupted; the program should have only changed the local short integer. This problem does not occur inside the QuickBasic QB.EXE environment.

STATUS

Microsoft has confirmed this to be a bug in QuickBasic Versions 4.00, 4.00b, and 4.50 and Microsoft Basic Compiler Versions 6.00 and 6.00b (buglist6.00, buglist6.00b) for MS-DOS and MS OS/2. This problem was corrected in Microsoft Basic Compiler Version 7.00 (fixlist7.00).

This problem does not occur when the program is compiled with the debug option (BC /D). To work around the problem, compile with the debug option.

MORE INFORMATION

The following steps will demonstrate this problem:

  1. Pass an integer to a subprogram whose formal parameter is declared to be a LONG integer.
  2. Within the subprogram, initialize a temporary, short-integer variable, and assign the LONG integer parameter to it. This forces a type conversion from LONG to short integer.
  3. Immediately print out the LONG integer parameter. Even though the program was not designed to change the LONG variable, it is corrupted (i.e., an unexpected value appears). The temporary, short-integer variable prints correctly.

The program code example below will work as expected inside the QuickBasic QB.EXE environment. However, when compiled into an executable (.EXE file), the problem occurs. Note that all three PRINT statements should return the same values (4).

The following is a code example:

DECLARE SUB mysub (long1&, long2&)
CLS
CALL mysub(4&, 4&)  ' Long or short integer constants both show problem.

SUB mysub (long1&, long2&)
PRINT "initial parameters: ", long1&, long2&
temp1% = 0
temp2% = 0
temp1% = long1&   ' It is here where the type conversion takes place
temp2% = long2&   ' and long1& and long2& are corrupted.
PRINT "Parameters after assignment: "; long1&, long2&
PRINT "Temporary variables after assignment: "; temp1%, temp2%
END SUB
                


Additional query words: QuickBas BasicCom buglist4.00 buglist4.00b buglist4.50 B_BasicCom

Keywords: KB37305