matthew@cs.ua.oz.au (Matthew Donaldson) (08/03/90)
I have had some rather strange behaviour from AT&T C++ 2.0 when dynamically
allocating and freeing arrays of classes. When newing an array of
classes, the constructor is called for each element created, but when
deleteing the array, only one destructor is called. Everything works
as expected, however, if I declare an array on the stack.
Here is the code I used:
class X
{
public:
X() { puts("Construct"); }
~X(){ puts("Destruct"); }
};
main()
{
X *Ar= new X[10];
delete Ar;
}
( Prints 10 "Construct" and 1 "Destruct" )
If I replace main with:
main()
{
X Ar[10];
}
I get 10 "construct" and 10 "destruct".
Is this the way it is supposed to work, or am I doing something wrong?
Thanks in advance,
Matthew
--
-------
Matthew Donaldson
matthew@cs.ua.oz.au
Operating Systems Group
Computer Science Department
University of Adelaideark@alice.UUCP (Andrew Koenig) (08/04/90)
In article <MATTHEW.90Aug3233620@chook.ua.oz.au>, matthew@cs.ua.oz.au (Matthew Donaldson) writes: > Here is the code I used: > main() > { > X *Ar= new X[10]; > delete Ar; > } You should say X *Ar = new X[10]; delete[10] Ar; With cfront 2.1, you can say delete[] Ar; but the brackets are still essential. -- --Andrew Koenig ark@europa.att.com
burley@world.std.com (James C Burley) (08/05/90)
I don't program in c++ yet, but based on postings I've seen on these
boards, try saying "delete [10] Ar;" or "delete [] Ar;". You might
get your 10 destructions that way!
tq vm, (burley)
P.S. Tried sending this as email, but it got returned. At least if I'm
wrong in public, somebody'll correct me!