Path: utzoo!attcan!uunet!samsung!emory!att!dptg!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c++ Subject: Re: ``delete[]p'' -- where is the actual size of *p stored? Message-ID: <11255@alice.UUCP> Date: 30 Aug 90 16:51:13 GMT References: <56956@microsoft.UUCP> <1180@fornax.UUCP> Distribution: comp Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 29 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. > 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. Indeed. However, p is being used here as a pointer to the initial element of an array, and that makes all the difference. If you allocate an array of objects, you must free it through a pointer to the same type. Therefore this is illegal: B* p = new D[100]; delete [] p; For that matter, this won't work either: for (int i = 0; i < 100; i++) p[i] = /* something */ ; -- --Andrew Koenig ark@europa.att.com