Microsoft KB Archive/80184

= Microsoft Knowledge Base =

String Space Corrupt When REDIM PRESERVE on Fixed String Array
Last reviewed: January 12, 1995

Article ID: Q80184

The information in this article applies to:

- Microsoft Basic Professional Development System (PDS) for MS-DOS

and MS OS/2, version 7.1

SYMPTOMS
Performing a REDIM PRESERVE on a dynamic fixed length string array, using near strings in a stand alone program, and where the array size increases with each subsequent call of the REDIM PRESERVE statement, may result in a loss of space in the default data segment (DGROUP) or a &quot;String Space Corrupt&quot; error message.

To avoid this situation, avoid the use of REDIM PRESERVE on dynamic fixed length string arrays in a stand-alone program compiled for near strings.

STATUS
Microsoft has confirmed this to be a bug with Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2, version 7.1. This problem was corrected in Microsoft Visual Basic version 1.0 for MS-DOS.

MORE INFORMATION
Compiling with the /D (produce debug code) or /X (error trapping with RESUME) compiler options may be used to work around this problem.

Below are two code examples that duplicate this problem.

Example 1
' Compile with BC /ah or /ah/o to demonstrate problem. FOR N = 1 TO 32766 STEP 16

IF INKEY$ = &quot;q&quot; THEN STOP REDIM PRESERVE X(N) AS STRING * 64 LOCATE 20,1 PRINT &quot;N&quot;, &quot;FRE('')&quot;, &quot;FRE(-1)&quot; LOCATE 21,1 PRINT N, FRE(&quot;&quot;), FRE(-1) NEXT N END

Running the above program compiled with the /Ah option will show that REDIM PRESERVE on a dynamic fixed length string array may incorrectly allocate space in DGROUP before exhausting the far heap. This behavior may result in an unexpected &quot;Out of String Space&quot; error message.

Example 2
' Compile only with /O to demonstrate the problem. LoopCnt = 0 FOR i = 1 TO 100

Count = 0 FOR j = 1 TO 3 Count = Count +1 REDIM PRESERVE A(1 TO Count) AS STRING REDIM PRESERVE B(1 TO Count) AS STRING * 4 A(j) = &quot;aaaaaaaa&quot; B(j) = &quot;bbbb&quot; LoopCnt = LoopCnt + 1 LOCATE, 1 PRINT LoopCnt; FRE(A(1)); FRE(-1); FRE(-2); STACK NEXT J NEXT I

The above program will either hang or report a &quot;String Space Corrupt&quot; error message.