Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!apple!netcom!sjsumcs!horstman From: horstman@mathcs.sjsu.edu (Cay Horstmann) Newsgroups: comp.lang.c++ Subject: Re: Why no renew Message-ID: <1991Mar21.163616.20136@mathcs.sjsu.edu> Date: 21 Mar 91 16:36:16 GMT References: <1991Mar16.172814.6525@mathcs.sjsu.edu> <966@elan.Elan.COM> Organization: San Jose State University - Math/CS Dept. Lines: 36 In article <966@elan.Elan.COM> tom@elan.Elan.COM (Thomas Smith) writes: >>> >>> 1) Zeroing is infrequently needed and seldom even helpful. >> >> Huh? When you say X** a = new X*[100], isn't it most likely that you want >> all those X*'s to be null pointers? > >No. You're array might be null-terminated - in other words, you might do > X** a = new X*[10000]; // notice the extra zeros > a[0] = 0; // initialy empty - null first object >The extra effort supplied by the compiler second-guessing what I the >programmer intended to do is wasted effort. Furthermore, you may be >allocating the array to be large enough to handle a possible extreme case >in your program, rather than normal cases, thus it may need to be much >larger than 100. Zeroing out 10s or 100s of thousands of bytes is not >a trivial operation efficiency-wise. > This is an extremely rare case. In C++, it is not at all common to allocate such a large array right away just to handle a possible extreme case. It is much smarter to start out with a smaller array and grow it as needed with a variable array class. If you must allocate such a large array, and the penalty for zeroing it out is unacceptable, you can call malloc. To inject a little more realism into the discussion: Most processors have instructions that can zero out a memory block extremely quickly. But if you have lots of large blocks of memory "just in case", your program is much more likely to have to page them out, which is a zillion times more expensive. I continue to propose that operator new zero out arrays of non-class type, and that programmers who find that unacceptable use malloc. Cay