SHARED Dynamic Array Element Passed as Parameter Aliases to 0 (47566)



The information in this article applies to:

  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.0

This article was previously published under Q47566

SUMMARY

If you attempt to alter an element of a SHARED or COMMON SHARED $DYNAMIC array while inside a SUBprogram and that element has also been passed as a parameter to the SUBprogram, the value returned in the parameter will be the value assigned to the array element upon exit from the SUBprogram, and will replace whatever value may have been assigned directly to the array in the SUBprogram.

This behavior occurs in Microsoft QuickBasic versions 4.00, 4.00b, and 4.50, Microsoft Basic Compiler 6.00 and 6.00b for MS-DOS and MS OS/2, and Microsoft Basic PDS 7.00 for MS-DOS and MS OS/2.

This is a form of variable aliasing, which is a programming practice not recommended by Microsoft. A variable passed in an argument list to a procedure should not also be shared by means of the SHARED statement or the SHARED attribute (of the DIM or COMMON statement) in that procedure's module.

Similarly, a variable should not be passed twice in the list of arguments passed to a procedure, or else variable aliasing problems occur. This information can be found under "The Problem of Variable Aliasing" section, on Page 64 in the "Microsoft Basic 7.0: Programmer's Guide" for Basic PDS versions 7.00 and 7.10, on Page 68 of the "Microsoft QuickBasic 4.5: Programming in Basic" manual, and on Page 78 of "Microsoft QuickBasic 4.0: Programming in Basic: Selected Topics" manual for versions 4.00 and 4.00b.

MORE INFORMATION

The behavior in the first example above results from the way QuickBasic accesses $DYNAMIC arrays. Since access to these arrays requires a far pointer, and all parameters use near pointers, a temporary location is set aside for the parameter before and during the CALL, and the value in this location is assigned to the actual array element following the return from the CALL. Thus, whatever value you assign to the array element inside the SUBprogram using the SHARED array name will be replaced with the value of the parameter when the SUBprogram ends.

Note that when inside the QuickBasic environment, virtually all arrays are treated as $DYNAMIC. It is only at compile time that $STATIC arrays are actually made $STATIC by allocation in the data segment. Since the QuickBasic editor treats most arrays as $DYNAMIC, this behavior can be observed in the editor even for arrays declared $STATIC. Please see Pages 32-33 in the "Microsoft QuickBasic 4.0: Basic Language Reference" manual for versions 4.00 and 4.00b, and Pages 719-721 in the "Microsoft Basic 7.0: Programmer's Guide" for Basic PDS Version 7.00, for a complete description of where arrays are stored.

The following example demonstrates this behavior:
DECLARE SUB test (a!)
CLS
' *** The DYNAMIC metacommand is not required to reproduce
'     this behavior inside the QuickBasic environment.
'$DYNAMIC
DIM SHARED b(20)        'Step 1. b(20) initially is 0.
CALL test(b(20))
PRINT b(20)             'Step 3. PRINTs 0 -- global has been
END                     '        replaced by returned value.

SUB test (a!)
  b(20) = 10
  PRINT b(20)           'Step 2. PRINTs 10 -- Global value is
END SUB                 '        changed. Parameter a! is still 0.
				

Modification Type:MinorLast Reviewed:1/9/2003
Keywords:KB47566