Overloading the __huge new Operator

Last reviewed: July 17, 1997
Article ID: Q102229
1.00 WINDOWS kbprg

The information in this article applies to:

  • Microsoft Visual C++ for Windows, version 1.0

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


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.