Problems Not Including MALLOC.H in Compact, Large Model

ID: Q42756

6.00 6.00a 6.00ax 7.00 | 6.00 6.00a | 1.00 1.50

MS-DOS                 | OS/2       | WINDOWS
kbprg

The information in this article applies to:

SUMMARY

In the compact and large memory models, it is vital that the malloc() function be prototyped as returning a void far pointer. The recommended method is to include the header file MALLOC.H. This file contains the prototype for malloc().

The default data-pointer size in the compact and large models is 32 bits. The default return type of any function, including malloc(), when it is not prototyped is a 16-bit short integer. Thus, the segment portion of a far address will be destroyed unless the compiler knows it is dealing with a 32-bit type. The compiler versions 5.1, 6.0, 6.0a, and 6.0ax will generate the following warnings if the warning level is set high enough:

   C4016: 'malloc': no function return type, using int as default
   C4017: 'malloc': no function prototype given

Microsoft C/C++ version 7.0 and Visual C++ version 1.0 and 1.5 will generate the following warning:

   C4013: 'malloc' undefined: assuming extern returning int

It is a good idea to compile with the highest warning level so that problems like this can be caught at compile time.

MORE INFORMATION

If you attempt to use the UNIX coding style of including MEMORY.H rather than MALLOC.H, you will encounter problems at run time. MEMORY.H does not prototype any of the memory allocation functions; it prototypes only memory copy and move functions. In a segmented architecture that can have data and code pointers of different sizes, this may have serious ramifications.

The compiler will generate warnings for the following sample code:

Sample Code:

/* Compile options needed: /AL /W4
*/ 

#include <memory.h>

void main()
{
   char    *addr;
   size_t  nbytes = 100;

   addr = (char *) malloc( nbytes );
}

Additional reference words: kbinf 6.00 6.00a 6.00ax 7.00 1.00 1.50 KBCategory: kbprg KBSubcategory: CLngIss Keywords : kb16bitonly

Last Reviewed: July 18, 1997