FIX: C2466: Using New to Allocate an Array of Size Zero

Last reviewed: September 18, 1997
Article ID: Q123811
7.00 | 1.00 1.50 1.51 | 1.00
MS-DOS | WINDOWS        | WINDOWS NT
kbtool kbfixlist kbbuglist

The information in this article applies to:

  • The Microsoft C/C++ Compiler (CL.EXE) included with:

        - Microsoft C/C++ for MS-DOS, version 7.0
        - Microsoft Visual C++ for Windows, versions 1.0, 1.5, and 1.51
        - Microsoft Visual C++, 32-bit Edition, version 1.0
    

SYMPTOMS

If a constant expression that evaluates to zero is used to allocate an array, the following error occurs:

   error C2466: cannot allocate an array of constant size 0

RESOLUTION

To work around the problem, use a variable that is initialized to zero in place of the constant when allocating the array.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug was corrected in Visual C++, 32-bit Edition, version 2.0.

MORE INFORMATION

The operator new() can be called with the argument zero. A distinct (non- null) pointer to an object will be returned (see Section 5.3.3 of the ARM - M.A.Ellis, B.Stroustrup). This is the behavior observed with Visual C++ version 2.0.

The following sample code can be used to demonstrate the problem.

Sample Code to Reproduce Problem

/* Compile options needed: none
*/

char * p = new char[0];  // Causes C2466


Additional reference words: 1.00 1.50 7.00 8.00 8.00c
KBCategory: kbtool kbfixlist kbbuglist
KBSubcategory: CPPIss

Keywords : CPPIss kbbuglist kbfixlist kbtool
Version : 7.00 | 1.00 1.50 1.51 | 1.00
Platform : MS-DOS NT WINDOWS
Solution Type : kbfix


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: September 18, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.