From -7640759464697299463
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,afd7c5d805e2c577
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 1993-03-25 03:16:04 PST
Path: sparky!uunet!mcsun!marble.uknet.ac.uk!uknet!zaphod.axion.bt.co.uk!bnr.co.uk!demon!dspcproj.demon.co.uk!philr
From: philr@dspcproj.demon.co.uk (Phil Reynolds)
Newsgroups: comp.std.c++
Subject: Re: Is delete [] always safe?
Message-ID: <733083364snx@dspcproj.demon.co.uk>
Date: 25 Mar 93 11:16:04 GMT
References: <1993Mar22.024915.14564@fcom.cc.utah.edu>
Sender: usenet@demon.co.uk
Organization: Datastream International Ltd
Lines: 31
ReplyTo: philr@dspcproj.demon.co.uk
X-Mailer: cppnews $Revision: 1.19 $


In article <1993Mar22.024915.14564@fcom.cc.utah.edu> dave@csulx.weber.edu writes:

> Is it safe to do a delete [] pointer when pointer is not a vector?
[ Rest of post deleted ... ]
> 
> --
> dave@csulx.weber.edu (sig by ByteWatchers & TransmitFast)
> 

I shall try to explain why this is not safe!

The syntax 'new Class [ Size ]' causes the compiler to generate
constructor calls for each element in the array. As well as
doing this it adds some information to the block of memory
allocated to tell it how many items there are in the array. This
is so when you use the delete [] syntax, it knows how many
destructors to call and how much memory to free.

Now it may be that your compiler always treats 'new Class' as
'new Class[1]' and thus delete [] will work but don't rely on it.

P.S. In the old days (when I were a lad) you had to provide the
size of the array to the delete operator e.g. delete [ Size ] pArray.

+-------------------------------------+
|Phil Reynolds                        |
|PC Projects                          |
|Datastream International Ltd.        |
|philr@dspcproj.demon.co.uk (I think!)|
+-------------------------------------+


