PRB: C2710: Cannot Delete a Pointer to a Const Object

Last reviewed: August 26, 1997
Article ID: Q131322
The information in this article applies to:
  • The Microsoft C/C++ Compiler (CL.EXE) included with: - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52 - Microsoft Visual C++, 32-bit Edition, versions 2.0, 2.1, 4.0, 5.0

SYMPTOMS

Attempting to delete a pointer to a constant causes the compiler to generate the following correct error message:

   Error C2710 : cannot delete a pointer to a const object

CAUSE

Deleting a pointer to a constant should not be allowed by definition (ARM section 5.3.4) because it modifies the object pointed to. However, if you deliberately or accidentally use the explicit conversion, the compiler doesn't generate the error. The consequences of a such attempt are unpredictable and compiler implementation dependent.

STATUS

This behavior is by design.

MORE INFORMATION

Sample Code #1 to Demonstrate Behavior

   /* No special compile options needed. */

   const int * pi   = new int(1000);
   const char * pch = new char(65);

   void main(void)
   {
     delete  pi  ;// Error C2710:cannot delete a pointer to a const object
     delete  pch ;// Error C2710:cannot delete a pointer to a const object
   }

Visual C++ version 5.0 generates the following error:

   Error C2664: 'delete' : cannot convert parameter 1 from 'const char *'
   to 'void *'

Sample Code #2 to Demonstrate Behavior

   /* No special compile options needed.
      The type casting of the pointer to const argument of the delete
      operator prevents the C2710 error but is not recommended. */

   const int * pi   = new int(1000);
   const char * pch = new char(65);

   void main(void)
   {
      delete (int *) pi;   //no error C2710 generated
      delete (char *) pch; //no error C2710 generated

   }

REFERENCES

For more information, see The Annotated C++ Reference Manual (1994) section 5.3.4.


Additional query words: 8.00 8.0 8.0c 8.00c 9.00 9.0 9.1 9.10
Keywords : CPPIss kbcode
Version : WINDOWS:1.0,1.5,1.51,1.52; WINDOWS NT:2.0,2.1,4.0,5.0
Platform : NT WINDOWS
Issue type : kbprb


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