[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1917
  • Last Modified:

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...
0
dennis_george
Asked:
dennis_george
  • 6
  • 3
  • 2
  • +4
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now