ENVIRON Statement Produces "Out of Memory" Error Message (21963)






This article was previously published under Q21963

SUMMARY

Most forms of the ENVIRON statement produce the run-time error message "Out of memory" (on DOS versions 2.x through 3.30). The following example produces the "Out of memory" error message:
   ENVIRON "PATH=C:\COBOL"
				
The following example usually works correctly; however, the ENVIRON statement is not very practical in QuickBasic:
   ENVIRON "PATH=;"
				
Expanding the environment table cannot be done in a QuickBasic program because the size of the environment table is limited to its size rounded up to the nearest multiple of 16 bytes, when QuickBasic or a QuickBasic program is initiated.

QuickBasic does not modify the actual DOS environment table; it only modifies a local copy.

MORE INFORMATION

You can work around this problem by doing the following:

  1. SET a large string in the MS-DOS environment table before running QB.EXE or a compiled program, as in the following example:
       SET JUNK=123456789012345678901234567890
  2. Use the following ENVIRON statements in your program to set the large string equal to a null string, then you can allocate strings whose length totals up to the size that was deallocated:
       PRINT ENVIRON$("JUNK")  ' DOS environment variables MUST be
                               ' uppercase.
    
       ENVIRON "JUNK=;"        ' This deallocates environment table
                               ' space.
    
       PRINT ENVIRON$("JUNK")  ' This confirms deallocation; prints
                               ' nothing.
    
       ENVIRON "TEST=C:\COBOL" ' This sets up new environment string.
    
       PRINT ENVIRON$("TEST")  ' This displays the new environment
                               ' string.
    						
    Note that the MS-DOS environment variable names must be uppercase.

How to Make Basic Change Environment After Exiting

To make the Basic program change the DOS environment after ending, you can perform the following steps using MS-DOS batch files:

  1. Invoke your QuickBasic program from an MS-DOS batch file.
  2. At run time, your QuickBasic program can create a text file containing MS-DOS batch commands to SET environment variables.
  3. After the QuickBasic program ends and returns control to the batch file, the batch file can use the batch Call statement to invoke the batch (text) file created by the QuickBasic program.

Modification Type: Minor Last Reviewed: 1/9/2003
Keywords: KB21963