Error message in Visual C++ when you delete a pointer to a const object:

Article translations Article translations
Article ID: 131322 - View products that this article applies to.
This article was previously published under Q131322
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all

On This Page

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
NOTE: Visual C++ .NET compiler does not demonstrate this issue in conformance to the changes made in the C++ ANSI Standards.

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 and 6.0 generate 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.

Properties

Article ID: 131322 - Last Review: February 28, 2014 - Revision: 5.0
APPLIES TO
  • Microsoft Visual C++ 1.0 Professional Edition
  • Microsoft Visual C++ 1.5 Professional Edition
  • Microsoft Visual C++ 1.51
  • Microsoft Visual C++ 1.52 Professional Edition
  • Microsoft Visual C++ 1.0 Professional Edition
  • Microsoft Visual C++ 2.0 Professional Edition
  • Microsoft Visual C++ 2.1
  • Microsoft Visual C++ 4.0 Standard Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++, 32-bit Learning Edition 6.0
  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition
  • Microsoft Visual C++ 2005 Express Edition
Keywords: 
kbnosurvey kbarchive kberrmsg kbtshoot kbprb kbcpponly kbcode kbcompiler KB131322

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com