FUNCTIONs and Subprograms Can Affect FIELDed Arrays in COMMON (29923)






This article was previously published under Q29923

SYMPTOMS

An array that is in both a FIELD and a COMMON statement can be adversely affected if you pass it as a parameter to a FUNCTION or subprogram procedure.

The program below has two FIELD statements that result in two arrays pointing to the same memory location. However, after a call to a FUNCTION that should not affect the arrays, they no longer point to the same place in memory. Compiling with debug (BC /D) does not help.

STATUS

Microsoft has confirmed this to be a bug in QuickBasic Versions 4.00, 4.00b, and 4.50, in Microsoft Basic Compiler Versions 6.00 and 6.00b for MS- DOS and MS OS/2, and in Microsoft Basic Professional Development System (PDS) Version 7.00 for MS-DOS and MS OS/2. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

The program works correctly in QuickBasic Version 3.00 if the FUNCTION is made into a subprogram. (QuickBasic Version 3.00 does not support FUNCTION procedures.)

The following is an example using a FUNCTION, but the problem also occurs if the FUNCTION is made into a subprogram:
DECLARE FUNCTION Nothing% (any$)
DEFINT A-Z
COMMON SHARED /GLOBAL/ field1$(), field2$()
DIM field1$(20), field2$(20)
CLS
OPEN "anyfile.tmp" FOR RANDOM SHARED AS #1 LEN = 10
FIELD #1, 10 AS field1$(1)
FIELD #1, 10 AS field2$(1)

'** NOTE: At this point, field1$(1) and field2$(1) should point to    **
'** the same storage location (buffer space) as verified below.       **

LSET field1$(1) = "ABC"
PRINT field1$(1); field2$(1)
LSET field2$(1) = "DEF"
PRINT field1$(1); field2$(1)

x = Nothing(field1$(1))     '** prints field1$ but should not change it. **
'x = Nothing((field1$(1)))  '** passes by value -- this works **

'** Now notice that field1$(1) no longer points to the same storage as  **
'** its counterpart, field2$(1), as verified below.                     **

LSET field1$(1) = "ABC"
PRINT field1$(1); field2$(1)
LSET field2$(1) = "GHI"
PRINT field1$(1); field2$(1)
CLOSE
END

FUNCTION Nothing (any$)
        x$ = any$       '** so won't affect value of any$ **
        PRINT x$
END FUNCTION
				

Modification Type: Minor Last Reviewed: 1/8/2003
Keywords: KB29923