Path: utzoo!telly!attcan!uunet!microsoft!jimad From: jimad@microsoft.UUCP (Jim ADCOCK) Newsgroups: comp.lang.c++ Subject: Re: ``delete[]p'' -- where is the actual size of *p stored? Message-ID: <57165@microsoft.UUCP> Date: 4 Sep 90 20:03:20 GMT References: <56956@microsoft.UUCP> <1180@fornax.UUCP> Reply-To: jimad@microsoft.UUCP (Jim ADCOCK) Distribution: comp Organization: Microsoft Corp., Redmond WA Lines: 27 In article <1180@fornax.UUCP| miron@fornax.UUCP (Miron Cuperman) writes: |jimad@microsoft.UUCP (Jim ADCOCK) writes: | |>In article cline@sun.soe.clarkson.edu (Marshall Cline) writes: |>> B* p = new D[100]; |>> delete [] p; | |>C++ will already "do the wrong thing" with any attempts to address the d's |>referred to by p, thus rendering any problems with delete [] p moot. |>The right thing is for a compiler to issue a stern warning on the |>new array statement assigned to an incompatible pointer. | |This is not correct. A pointer to an object may be cast to a pointer |to a base class of that object. Therefore the above syntax is correct. |From what I understand, the C++ implementation must save the length of |the array in bytes, and deallocate it accordingly. You did not read what I said. I said any attempts to *address* the d's referred to by p will cause "the wrong thing" to happen. The ability to cast to a base class pointer is a far different thing from being able to address an array of Ds as if they were an array of Bs. Calling delete [] p for an array of Ds with p a B*, will typically cause some number of B destructors to be invoked -- which is presumably not what the programmer intended. Compilers will accept this syntax, and generate code other than what the programmer presumably intended. I consider this a weakness in the C++ type system -- which is best plugged by a compiler issuing a warning: "Do you really want me to generate this nonsensible code?"