"Out of String Space" Concatenating Variable-Length String (45687)



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

This article was previously published under Q45687

SUMMARY

For variable-length string concatenation to execute successfully, even if only 1 byte is to be added, there must be enough available memory [reported by FRE("")] for the length of a copy of the existing string, plus the length of the string being added, plus its new 4-byte string descriptor. Otherwise, you will get an "Out of String Space" error, caused by the temporary old string remaining in memory during the string concatenation. The old string is deallocated only after a successful concatenation.

This information applies to QuickBasic Versions 1.00, 1.01, 1.02, 2.00, 2.01, 3.00, 4.00, 4.00b, and 4.50, and to Basic Compiler Versions 6.00 and 6.00b for MS-DOS and OS/2.

This information also applies to Microsoft Basic PDS Version 7.00 when using BC.EXE without the /Fs compiler switch. Inside the QBX.EXE environment and when using /Fs compiler switch, Basic 7.00 uses a 64K segment for temporary string storage and procedure-level strings (that is, strings declared inside subroutines). To see how much temporary string space you have when using far strings, use FRE("StringLiteral"), where StringLiteral is any constant string (for example, "Hello"). For more information about far strings, read Chapter 11 in the "Microsoft Basic 7.0: Programmer's Guide" for Basic PDS Version 7.00.

MORE INFORMATION

When you concatenate a variable-length string with another string in Basic, a new 4-byte string descriptor is created. A copy of the original string is moved to a new location in string space and the string to be concatenated is appended to it. The length field in the new string descriptor is updated to reflect the length of the concatenated string. Only then is the memory used by the original string and its descriptor released (deallocated).

New strings are allocated above existing strings and deallocated strings in DGROUP. When deallocated strings fragment string space to the point where a new string fills the last space at the top of DGROUP, Basic automatically performs string space compaction (garbage collection) to make free string space contiguous again. Passing a string argument (such as "", the null string) to the FRE("") function always forces string space compaction before reporting the amount of string space available in DGROUP.

For example, assume FRE("") returns 20,004. An attempt to add a single byte to an existing string of 20,000 bytes would fail because 20,005 bytes are needed for the concatenation to be successful:
   Existing string   = 20,000 bytes
   String to add     =      1 byte longer
   String descriptor =      4 bytes
                    -----------
   Memory needed       20,005 bytes
				
Because only 20,004 bytes of memory are available, an "Out of String Space" message will be generated.

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