We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

VC6 - deleting const char*

dennis_george
on
Medium Priority
2,432 Views
Last Modified: 2008-02-01
Hi all,

Is the following behaviour is also because the VC6 does no compy to the latest C++ standards (pro 1998)....

      const char* ptr = new char[SIZE];

      delete ptr; // compiler error...

<<ERROR>>
error C2665: 'delete' : none of the 2 overloads can convert parameter 1 from type 'const char *'
Error executing cl.exe.
<<ERROR>>

does always I have to typecast the pointer and then delete the variable...... as..

delete const_cast<char*> (ptr);

PS: I know the code given above doesn't make sense as such because you can't change the data afterwards.... but there might be scenario where some allocation function returns you const char* and the function does not take the ownership...
Comment
Watch Question

In some cases  using const_cast makes sense.It is also  of the reason being for having const_cast  in C++ .

Commented:
Hi dennis_george.

>> does always I have to typecast
>> the pointer and then delete the >> variable...... as..
>> delete const_cast<char*> (ptr);

yes, this is the standart
const_cast is a way  to  take away the constness.
You need to use it always  in this case
" const char* ptr = new char[SIZE];"
but if the case is say using placement new .
char a[50];
const char* ptr = new(a) char[50];

here
delete const_cast<char*> (ptr);
 will not make any sense , bcoz a is an automatic array.

So it depends from one case to another.
CERTIFIED EXPERT

Commented:
hi dennis_george,

and last but not least, always remember to delete an array this way:

delete[] array;

ike

Author

Commented:
But in g++ i can delete a const char* pointer without typecasting.....

>>yes, this is the standart
Hi, B1-66ER
do you mean that it is the part of c++ standards to delete a const pointer by typecasting it?

AxterSenior Software Engineer

Commented:
>>do you mean that it is the part of c++ standards to delete a const pointer by typecasting it?

No.

IAW C++ standard, you're allowed to delete a constant type pointer.
Any compiler that gives you a compile error, is not in compliance with the standard.

VC++ 6.0 is a prestandard compiler, which was developed before the official standard was published.
>>>> because the VC6 does no compy to the latest C++ standards

No, the delete operator is defined as

     void operator delete( void *pvMem )

you see it takes a non-const pointer as argument. Thus you need to cast to non-const before you can delete.

    char* p = (char*)ptr;  // you may use const_cast<char*> alternatively
    delete [] p;   // use [] when deleting an array

Regards, Alex

In VC7 this is accepted:

   const char * cTest = new char[25];
   delete [] cTest;

in VC6 you need:

   const char * cTest = new char[25];
   delete [] const_cast<char *>(cTest);

Which one is correct according to standards, I don't know. But I would guess both work when they compile.

My major point in writing here is to remind you that I would make _very_ sure before I freed a const char* returned from a function. Normally, this would rather indicate that the function wants to handle this itself, like e.g.:

const char * GetString()
{
   static char sTheString[1024];
   // Give it some value
   return sTheString;
}

freeing this return value is not good!
>>>> IAW C++ standard, you're allowed to delete a constant type pointer.

Sorry, I didn't refresh for some time.

Could you tell where I could verify that?

Regards, Alex
AxterSenior Software Engineer

Commented:
>>Which one is correct according to standards, I don't know. But I would guess both work when they compile.

VC7 is correct IAW the C++ standard.

The code you're using for VC6 is an acceptable workaround, but IAW C++ standard it should not be needed if the compiler is in compliance.
AxterSenior Software Engineer

Commented:
>>Could you tell where I could verify that?

I don't have a copy of the standard in my current location, but check section 12.4 and section 5.3.5.4
Senior Software Engineer
Commented:
From section 12.4, paragraph 2
*****************************************************
A destructor is used to destroy objects of its class type. A destructor takes no parameters, and no return
type can be specified for it (not even void). The address of a destructor shall not be taken. A destructor
shall not be static. A destructor can be invoked for a const, volatile or const volatile
object.
*****************************************************

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
>> do you mean that it is the part of
>> c++ standards to delete a const
>> pointer by typecasting it?

not entirely dennis_george :) i just want to say that before pass constant pointer to delete() you need make typecasting.

Delete operator defined like

void operator delete( void *pvMem )

so according to standards, you need
typecasting :)
AxterSenior Software Engineer

Commented:
>>so according to standards, you need
>>typecasting :)

That is not true.
IAW C++ standard, you don't need to typecast a constant pointer in order to delete it.
Compilers that force you to do this, are not in compliance with the C++ standard.

Commented:
Sorry, Axter I didnt see your previous messages.
AxterSenior Software Engineer

Commented:
FYI:
The VC++ 7.1 compiler is much more compliant to the standard than is the VC++ 6.0 compiler.
If you see something compiles differently between the two, than it's a high probability that the VC++ 7.1 compiler changed in order to be more compliant with the standard.

Author

Commented:
Is there any document published by Microsoft about VC7 or any of its development suit that whether the are compliant to the standard c++ or not?

By the way thanks all for the fruitful discussion...

Regards,
Dennis

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.