VC6 - deleting const char*

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...
LVL 5
dennis_georgeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DineshJolaniaCommented:
In some cases  using const_cast makes sense.It is also  of the reason being for having const_cast  in C++ .
0
B1-66ERCommented:
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
0
DineshJolaniaCommented:
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.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

ikeworkCommented:
hi dennis_george,

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

delete[] array;

ike
0
dennis_georgeAuthor 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?

0
AxterCommented:
>>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.
0
itsmeandnobodyelseCommented:
>>>> 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

0
Knut HunstadCommented:
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!
0
itsmeandnobodyelseCommented:
>>>> 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
0
AxterCommented:
>>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.
0
AxterCommented:
>>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
0
AxterCommented:
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.
*****************************************************
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
B1-66ERCommented:
>> 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 :)
0
AxterCommented:
>>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.
0
B1-66ERCommented:
Sorry, Axter I didnt see your previous messages.
0
AxterCommented:
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.
0
dennis_georgeAuthor 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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.