what's wrong?
Joern Dahl-Stamnes -- Jorn.Dahl-Stamnes@fysel.unit.no
Tuesday, January 09, 1996
Hello folks,
Yesterday I wrote some code and had a strange problem. I'm using Visual C++ 1.5
and my code looked like this:
const char** pStr;
pStr = new const char* [4];
.
.
.
delete [] pStr;
The compiler complain about the delete line. It say it cannot convert parameter
1. But if I write:
delete [] (void*) pStr;
the compiler does not complain.
My question is: WHY? What is wrong with the first delete statement?
--
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Joern Yngve Dahl-Stamnes, The University of Trondheim, Norway |
| e-mail: Jorn.Dahl-Stamnes@fysel.unit.no |
| phone : +73 59 44 12, fax: +73 59 14 41 |
| Surfing the net? Try http://www.fysel.unit.no/dahls/dahls.html |
| NetCrack not required! |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Dave Brooks -- info@brooksnet.com
Wednesday, January 10, 1996
[Mini-digest: 6 responses]
At 11:17 AM 1/9/96 +0100, Joern Dahl-Stamnes wrote:
>Yesterday I wrote some code and had a strange problem. I'm using Visual C++ 1.5
>and my code looked like this:
>
> const char** pStr;
>
> pStr = new const char* [4];
> .
> .
> .
> delete [] pStr;
>
>The compiler complain about the delete line. It say it cannot convert parameter
>1. But if I write:
>
> delete [] (void*) pStr;
>
>the compiler does not complain.
>
>My question is: WHY? What is wrong with the first delete statement?
While we often use ** as the address of an array of pointers, strictly
speaking it's not an array type to the compiler. "delete [] foo"
specifically refers to deleting foo where foo is declared as an array. You
don't actually need to use the [] operator, but supposedly it optimizes the
delete if foo is an array. I use it where I can, but haven't noticed any
adverse affects from not using it on non-array types.
I hope this helps!
Best regards,
Dave
**************************************************************************
David L. Brooks Brooks Internet Software, Inc.
info@brooksnet.com 1-800-523-9175 MST http://brooksnet.com/
Home of RPM, a new LPD server for Windows - check the Web page for demo!
-----From: bibhas@hermes (Bibhas Bhattacharya)
Can not delete const pointers.
Bibhas.
bibhas@isgtec.com
-----From: Mario Contestabile
This code compiles under 4.0.
-----From: Martijn Vels
I think it has to do with the idea of the delete[] operator.
I'm not sure about this, but here is my 'idea' why the compiler complains.
Think of const char * as object CFoo.
The delete[] operator will call all destructors in the array (. The
destructor of an object is not const, so you can't delete a const object.
What you have is an array of const objects. The quirk is that allocating
them whas the first fault which the compiler does not complain about.
Try this:
class CFoo;
const CFoo *p;
p = new const CFoo[2]
The compiler will directly nag you about the alloc.
The only way is:
class CFoo;
const CFoo *p;
p = new CFoo[2]
...
...
delete[] (CFoo *)p; /* Not const to us, we own it!!!! */
But, why botter?
The advantage of the const declarator is mainly for protecting objects you
pass into functions, or which you return as a result. (CString has therefore
no operator to autoconvert to char *!).
So the only point I see in using the const declarator is for protecting
yourself in this specific function.
M.
(vels0020@exact.nl)
-----From: David Stidolph
> const char** pStr;
> pStr = new const char* [4];
> .
> delete [] pStr;
The compiler complain about the delete line. It say it cannot convert parameter
1. But if I write:
delete [] (void*) pStr;
You need to delete each individual string (assuming you assign each pointer with a new):
for (int i=0; i < 4; i++)
delete []pStr[i];
In addition, you declared the pointer as const so that might cause your problem. Try not having the const declaration.
David Stidolph
-----------------------
Some assembly required.
-----From: snkavuri@amoco.com
This is a C++ question, may be VC++, certainly
not MFC :-) Coming to your question, the compiler is right.
In the first delete attempt, you tried deleting a const
object and it complained. C++ allows you to coerce a
const object to a nonconst one. You did that the next
time and it allowed you.
Surya Kavuri
| Вернуться в корень Архива
|