Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!bionet!sdsu!ucsdhub!hp-sdd!hplabs!hp-ses!hpcuhb!hpcllla!hpclisp!hpclscu!shankar From: shankar@hpclscu.HP.COM (Shankar Unni) Newsgroups: comp.lang.c++ Subject: Re: initializing array of objects Message-ID: <1000015@hpclscu.HP.COM> Date: 21 Jun 89 01:49:55 GMT References: <1284@cadillac.CAD.MCC.COM> Organization: Hewlett-Packard Calif. Language Lab Lines: 29 > bright@Data-IO.COM (Walter Bright) writes: > In article <1284@cadillac.CAD.MCC.COM> rpj@redcloud.cad.mcc.com (Rich Johns) writes: > >class Foo; > >Foo** fooArray; > >fooArray = new Foo*[100]; > >for (int i = 0; i < 100; i++) fooArray[i] = 0; > >Is there a better way to initialize fooArray? > > Try: > memset(fooArray,0,100 * sizeof(fooArray[0])); > or: > fooArray = (Foo**) calloc(100,sizeof(fooArray[0])); > Both are faster and generate less code. And both are non-portable. A NULL pointer does not have to have a bit-representation of all-zeroes. Admittedly this is rare, but I do know of a couple of such machines. "fooArray[i] = 0" initializes fooArray[i] to a NULL pointer value. "memset" and "calloc" initialize fooArray[*] to all-zero bits. Try the following: memset (fooArray, (Foo *) 0, 100*sizeof(Foo *)); It's fast and correct. ---- Shankar Unni.