Problems Not Including MALLOC.H in Compact, Large Model

Last reviewed: July 17, 1997
Article 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:

  • Microsoft C for MS-DOS, versions 6.0, 6.0a, and 6.0ax
  • Microsoft C for OS/2, versions 6.0 and 6.0a
  • Microsoft C/C++ for MS-DOS, version 7.0
  • Microsoft Visual C++ for Windows, versions 1.0 and 1.5

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


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.