Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ukma!rutgers!att!cbnewsl!dfp From: dfp@cbnewsl.ATT.COM (david.f.prosser) Newsgroups: comp.lang.c++ Subject: Re: initializing array of objects Message-ID: <885@cbnewsl.ATT.COM> Date: 22 Jun 89 17:56:38 GMT References: <1284@cadillac.CAD.MCC.COM> <1000015@hpclscu.HP.COM> Reply-To: dfp@cbnewsl.ATT.COM (david.f.prosser) Organization: AT&T Bell Laboratories Lines: 39 In article <1000015@hpclscu.HP.COM> shankar@hpclscu.HP.COM (Shankar Unni) writes: >> bright@Data-IO.COM (Walter Bright) writes: >> >> 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. > >Try the following: > > memset (fooArray, (Foo *) 0, 100*sizeof(Foo *)); > >It's fast and correct. >---- >Shankar Unni. Sorry. Your example is no better than the first use of memset(). The function sets *bytes* to the value of the middle argument. Unless the machine uses the same value for each byte of a pointer, this won't work. Try: static const Foo *empty[100]; (void)memcpy(fooArray, empty, sizeof empty); or static const struct foo100struct { Foo *array[100]; } empty; foo100struct *p; p = new foo100struct; *p = empty; fooArray = p->array; Both of these suffer from fixed sizes and both rely on the default initialization of pointers to null. (Apologies for any C++ usage errors; I am in no way an expert.) Dave Prosser