Microsoft KB Archive/45687

= &quot;Out of String Space&quot; Concatenating Variable-Length String =

Article ID: 45687

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

-



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(&quot;&quot;)] 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 &quot;Out of String Space&quot; 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(&quot;StringLiteral&quot;), where StringLiteral is any constant string (for example, &quot;Hello&quot;). For more information about far strings, read Chapter 11 in the &quot;Microsoft Basic 7.0: Programmer's Guide&quot; 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 &quot;&quot;, the null string) to the FRE(&quot;&quot;) function always forces string space compaction before reporting the amount of string space available in DGROUP.

For example, assume FRE(&quot;&quot;) 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 &quot;Out of String Space&quot; message will be generated.

Additional query words: QuickBas BasicCom

Keywords: KB45687

-

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

© Microsoft Corporation. All rights reserved.