Microsoft KB Archive/48059

= &quot;String Space Corrupt&quot; if BSAVE Variable-Length-String Array =

Article ID: 48059

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft QuickBasic 1.0
 * Microsoft QuickBasic 1.01
 * Microsoft QuickBasic 1.02
 * Microsoft QuickBasic 2.0
 * Microsoft QuickBasic 2.01
 * Microsoft QuickBasic 3.0
 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0

-



This article was previously published under Q48059



SUMMARY
If you want to use BSAVE and BLOAD with string arrays, you must use an array of fixed-length strings. Fixed-length strings are available in Microsoft QuickBasic versions 4.0, 4.0b, and 4.5 for MS-DOS, Microsoft Basic Compiler versions 6.0 and 6.0b for MS-DOS, and in Microsoft Basic Professional Development System (PDS) version 7.0, but not in earlier versions.

Arrays of variable-length strings CANNOT be BSAVEd to a file, nor can a file that was BSAVEd from a variable-length-string array be BLOADed into another variable-length-string array. A &quot;String space corrupt&quot; error message may be displayed if you attempt to BLOAD a file into a variable-length-string array, because the pointers in the BSAVEd string descriptors will overlay and tangle existing pointers to string space. This is the same mistake as POKEing a spurious value into a string descriptor, which can corrupt the integrity of string space.

This information applies to Microsoft QuickBasic versions 1.0, 1.01, 1.02, 2.0, 2.01, 3.0, 4.0, 4.0b, and 4.5 for MS-DOS, to Microsoft Basic Compiler versions 6.0 and 6.0b for MS-DOS and MS OS/2, and to Microsoft Basic PDS version 7.0 for MS-DOS and MS OS/2.



MORE INFORMATION
Each element of variable-length-string array has a 4-byte string descriptor composed of an offset (a 2-byte pointer) and length field (2 bytes). The array of string descriptors is stored sequentially, but the actual contents of the strings are stored separately in the dynamic string space. Each 2-byte offset points to a location in the string space. The string space memory is very dynamic, and strings are given new offsets whenever new string values are reassigned. The string contents of an array are not usually adjacent, especially if they have been reassigned values. As a result, BSAVEing a certain number of bytes does not mean that you've BSAVEd the contents of the variable-length-string array.



WORKAROUND
To work around this situation, create fixed-length-string arrays and BSAVE that information. Fixed-length-string space is allocated statically and sequentially in memory, and can be BSAVEd and BLOADed.

Code Example
The following code example attempts to BSAVE a variable-length-string array, but generates the error &quot;String Space Corrupt&quot; when run within the QuickBasic QB.EXE version 4.0, 4.0b, or 4.5 environment.

This example requires Microsoft QuickBasic version 4.0, 4.0b, or 4.5 for MS-DOS, Microsoft Basic Compiler version 6.0 or 6.0b for MS-DOS, or Microsoft Basic PDS version 7.0 for MS-DOS.

To alter this program to work correctly, change the DIMension statements to create a fixed-length-string array and BSAVE just that many bytes. OPTION BASE 1 DIM Arr1$(10)  ' Instead, use DIM Arr1(10) AS STRING*20 DIM Arr2$(10)  ' Instead, use DIM Arr2(10) AS STRING*20 ArrayLength% = 0 PRINT &quot;This is the BSAVE array:&quot; PRINT FOR I = 1 TO 10 <![CDATA[ Arr1$(I) = &quot;TEST&quot; + STR$(I) ArrayLength% = ArrayLength% + LEN(Arr1$(I)) PRINT Arr1$(I) NEXT I

DEF SEG = VARSEG(Arr1$(1)) ' In BC.EXE and QBX.EXE for Basic 7.0 use SSEG for far variable ' length strings.

BSAVE &quot;Test.Dat&quot;, VARPTR(Arr1$(1)), ArrayLength% DEF SEG PRINT PRINT &quot;Hit a Key&quot; PRINT SLEEP DEF SEG = VARSEG(Arr2$(1)) ' In BC.EXE and QBX.EXE for Basic 7.0 use SSEG for far variable ' length strings.

BLOAD &quot;Test.Dat&quot;, VARPTR(Arr2$(1)) DEF SEG PRINT &quot;This is the BLOADed array:&quot; PRINT FOR I = 1 TO 10 PRINT Arr2$(I) NEXT I

Additional query words: QuickBas BasicCom 1.00 2.00 3.00 4.00 4.00b 4.50 6.00 6.00b 7.00

Keywords: KB48059

-

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

© Microsoft Corporation. All rights reserved.