INF: malloc() and free() Do Not Return Memory to MS-DOS

ID Number: Q23869

3.00 4.00 5.00 5.10 6.00 6.00a 6.00ax 7.00

MS-DOS

Summary:

In Microsoft C versions 5.0, 5.1, 6.0, 6.0a, 6.0ax, and C/C++ version

7.0, when you malloc() a large amount of space (more than is available

within the current heap), the malloc() function will execute a DOS

call to get more memory in an attempt to expand the heap. If this is

successful, MS-DOS will allocate the desired memory to your program

and malloc() will return successful. If you then free() this memory,

it will be marked as free within your program's heap and will be

available for use again by your program.

However, MS-DOS still sees this memory as being used by the program

because it now resides within the program's heap. If you attempt a

subsequent system() call, MS-DOS may find it does not have enough free

memory to load the code required to perform the system() function.

With C versions 6.0 and later, the _heapmin() function is available

for the explicit purpose of returning unused space on the heap back to

the operating system.

Another alternative is to use halloc() and hfree(), which will return

the memory to MS-DOS, rather than retaining it for use by the currently

executing program.

Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 7.00