7

I got the following test code from http://support.microsoft.com/kb/131322:

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
   }

On that page Microsoft claims that deleting a pointer-to-const is not allowed, which seems logical to me. You don't want functions that you give a pointer-to-const to delete the instance behind your back.

Strange enough, question Deleting a pointer to const (T const*) indicates that it IS allowed, and it even makes sense.

And indeed, if I compile the code from the MSDN page with Visual Studio 2010, it compiles correctly (even no warnings when compiling with /W4).

Was the behavior regarding deletion of pointers-to-const changed in the past in the C++ standard? Or was this changed in Visual Studio?

Community
  • 1
  • 1
Patrick
  • 23,217
  • 12
  • 67
  • 130

3 Answers3

12

You can indeed delete a pointer-to-const.

If Visual C++ says otherwise for a standard-conforming program, then that's a compiler bug and should be reported.

Your (or Microsoft's?) program is not standard C++, however, since you have void result type for main.

The KB article you link to says "Deleting a pointer to a constant should not be allowed by definition (ARM section 5.3.4)", and although it's wrong, the reference it gives is correct. The ARM section 5.3.4 says "A pointer to constant cannot be deleted". However, the ARM was published in 1990...

C++ was standardized about ten years later, in 1998, and in standard C++ you can delete a pointer to const. This is not specified in the normative text; it's specified by omitting the restriction. However, the C++98 standard §5.3.5/2 has the following non-normative note:

a pointer to a const type can be the operand of a delete-expression; it is not necessary to cast away the constness (5.2.11) of the pointer expression before it is used as the operand of the delete-expression.

We are now over ten years after that standardization again, over 20 years after the ARM.

Which version of Visual C++ are you using?

Cheers & hth.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • Solaris Studio 12 C++ compiler suffers from the same bug that it does not allow deleting pointers to const. – Maxim Egorushkin Jan 28 '11 at 10:15
  • I am using Visual Studio 2010, which behaves correctly (to the latest standard, at least regarding this topic), but I was confused by the Microsoft article stating the opposite. But even then: const seems to indicate "Don't touch me; if you touch me you will get an error. You want to kill me? Ok, go on". Isn't this strange? – Patrick Jan 28 '11 at 10:19
  • 2
    @Patrick: not so strange. E.g. you can do `T const* p = new T const (42)`. In order to support initialization it's non-`const` during constructor execution, then turns `const`. And, if no casting is done, remains very `const` until you `delete`, where the destructor is allowed to modify in order to do cleanup. The original ARM-era rationale for disallowing, was that `delete` would change the object. The C++98 view is more sophisticated, that any very solid and unchanging house is changed during construction and during destruction... Cheers, – Cheers and hth. - Alf Jan 28 '11 at 10:24
  • @Patrick: the idea of `const` is that an object is not modified during its lifetime. The kicker here is the definition of lifetime (which differs from that of the lifetime of the storage): the lifetime begins once the constructor has successfully terminated and ends the instant the destructor begins. – Matthieu M. Jan 28 '11 at 10:29
  • I would say that with a const, the only thing your ask of it, is not to be value changed from the moment you set it. However since it will be deleted at some point (when the progrmas close by example) why not let the user decide when ? – lollancf37 Jan 28 '11 at 10:30
  • 2
    This code fails compile on VC6. However on VC9 it compiles just fine. So I guess it was a problem with the VC6 compiler. – Naveen Jan 28 '11 at 10:34
  • 2
    VC6 was released in 1998, months before the ISO C++98 standard. So it's not _entirely_ unreasonable that it followed the ARM here. – MSalters Jan 28 '11 at 11:15
  • 1
    @MSalters: Add to that the fact that back then MS had lost their voting rights in the standardization committee due to continuous absence from standardization meetings. VC6 was broken regarding the standard the moment it was released. (The standard was already set in stone by then and only required mere formalities until it was released officially.) But MS has certainly swung the steering wheel since (heck, they hired the former committee chair man) and VC has become a lot better. – sbi Jan 28 '11 at 13:32
2

You are allowed to delete a pointer to const and Microsoft obviously had it wrong previously. The standard has never changed, as far as I'm aware.

No doubt someone with a copy will quote chapter and verse (and get a big rep for it).

main should return int though, not void.

CashCow
  • 30,981
  • 5
  • 61
  • 92
1

Please also note that the Microsoft link itself says,

NOTE: Visual C++ .NET compiler does not demonstrate this issue in conformance to the changes made in the C++ ANSI Standards.

Interesting!

Nawaz
  • 353,942
  • 115
  • 666
  • 851