"String Space Corrupt" if BSAVE Variable-Length-String Array (48059)



The information in this article 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
  • 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 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 "String space corrupt" 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 "String Space Corrupt" 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 "This is the BSAVE array:"
PRINT
FOR I = 1 TO 10
<WWFIXEDTEXT><![CDATA[
   Arr1$(I) = "TEST" + 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 "Test.Dat", VARPTR(Arr1$(1)), ArrayLength%
DEF SEG
PRINT
PRINT "Hit a Key"
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 "Test.Dat", VARPTR(Arr2$(1))
DEF SEG
PRINT "This is the BLOADed array:"
PRINT
FOR I = 1 TO 10
   PRINT Arr2$(I)
NEXT I
				

Modification Type:MinorLast Reviewed:1/8/2003
Keywords:KB48059