INF: Problems Not Including MALLOC.H in Compact, Large Model

ID Number: Q42756

5.00 5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

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 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: 5.00 5.10 6.00 6.00a 6.00ax 5.0 5.1 6.0

6.0a 7.00 7.0