Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!elroy.jpl.nasa.gov!decwrl!world!wmm From: wmm@world.std.com (William M Miller) Newsgroups: comp.std.c++ Subject: Re: X3j16 "contractions" Message-ID: <1991Mar30.163059.20072@world.std.com> Date: 30 Mar 91 16:30:59 GMT References: <1991Mar30.065943.3074@lia> Organization: Glockenspiel, Ltd. Lines: 41 jgro@lia (Jeremy Grodberg) writes: > I'd like to add to the list of contractions the "feature" that a class' > overloaded operator new is not called when allocating an array of > class objects. This "feature" eliminates any clean way of handling, or > prohibiting, arrays of classes in situations where you want to do your > own memory allocation (or reference count, or debug tracing, or whatever). While I agree that some features might best be removed, this is not one of them. There are basically two arguments against calling X::operator new() for an array of X. The first is that an array of X is not a user-defined type, any more than a pointer to X is; arrays and pointers are built-in types, and you can't overload on built-in types (at least until/unless we adopt operator.() :-). If that philosophical point doesn't convince you, the pragmatic argument is that the biggest use of operator new() is to reduce the cost of storage allocation when some feature (typically constant size) of the object being allocated allows the programmer to do a smarter job than is possible for the general-purpose allocator. If operator new() had to worry about arbitrary- sized arrays instead of just fixed-size individual objects, the job of writing operator new() would become more complex and dangerous. (Okay, admittedly it's just the addition of a couple of lines of code like, if (size != sizeof(X)) return ::operator new(size); But if you forgot to write that -- and don't forget that there are by now thousands of operator new()s already written that *don't* have that code -- you have written *very* unsafe code; if it's ever used to allocate an array, you're guaranteed to go tromping through unowned memory.) Back to the philosophical side of things, C-style arrays are a very low-level kind of data structure. If you really want to do fancy things like reference counting, debug tracing, and the like, you ought to write your own Array container class and use it instead of plain vanilla arrays; it's not hard, and C++ gives you all the features you need to do so. -- William M. Miller, Glockenspiel, Ltd. wmm@world.std.com