Overloading the __huge new Operator

ID: Q102229

1.00 WINDOWS kbprg

The information in this article applies to:

When an application overloads the __huge version of operator new with C/C++ version 8.0 for MS-DOS, you must verify that the declaration properly specifies two parameters.

Microsoft confirmed a problem using the __huge operator new in C/C++ version 7.0 for MS-DOS and documented the methods to work around the problem in the Microsoft Knowledge Base. For more information, please search on the following words:

   huge operator new

MORE INFORMATION

An operator new that returns a huge pointer acts like an array allocator. It has two parameters: the number of elements to allocate and the size of each element. If the total size of the array is greater than 128K, the size of each element must be a power of 2.

The correct declaration for the __huge version of operator new is as follows:

   void __huge *operator new(unsigned long elems, size_t size);

Note that when calling new(), the first parameter must be a constant rather than a variable due to a code generation problem. Please see the following Knowledge Base article for a description of this problem:
   Q111754: BUG: __Huge New Operator Fails with Variable Size

Sample Code

/*
 * Compiler options needed: /AL /Od /Zi
 */ 

#include <iostream.h>
#include <malloc.h>
#include <stdio.h>

void __huge *operator new(unsigned long num, size_t size);

void __huge *operator new(unsigned long num, size_t size)
{
   cout << "Inside __huge new\n";
   return _halloc(num, size);
}

void main()
{
   int __huge *h_ptr;

   h_ptr = new __huge int[10000L];
   if (h_ptr == NULL)
      cout << "Alloc failed\n";

   delete h_ptr;
}

Additional reference words: kbinf 1.00 overload customizing KBCategory: kbprg KBSubcategory: CPPLngIss Keywords : kb16bitonly

Last Reviewed: July 18, 1997