Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!yale!slb-sdr!saito From: saito@sdr.slb.com (Naoki Saito) Newsgroups: comp.lang.c++ Subject: An array of objects again! Message-ID: <1990Feb9.201223.14332@sdr.slb.com> Date: 9 Feb 90 20:12:23 GMT Reply-To: saito@slb-sdr.UUCP () Organization: Schlumberger-Doll Research, Ridgefield CT Lines: 34 A long time ago, there was a discussion about the initializing the array of objects. My question is slightly different from that. Suppose I want to declare an array of object Cell. There are two ways to declare this. (1) array of pointers to objects Cell** ppc = new Cell*[N]; // null constructor. for (int i = 0; i < N; i++) ppc[i] = new Cell(i); // constructor with some argument. (2) array of objects Cell* pc = new Cell[N]; // null constructor. for (int i = 0; i < N; i++) pc[i] = *(new Cell(i)); // constructor with some argument. Now, the differences of these two approaches look like o The approach (1) needs N x (sizeof(Cell*) + sizeof(Cell)) memory space whereas (2) only needs N x sizeof(Cell). o The approach (2) creates temporary objects and calls assignment operator = i.e., pc[i] = *(new Cell(i)); is equivalent to Cell temp(i); pc[i] = temp; // assignment operator. ~temp(); whereas (1) simply copies the address to the pointers. So these are the trade-off. Am I missing something here? Does anyone has an idea which is better than which? Should the decision be made based on whether memory space or computation time is important? -- Naoki Saito (saito@sdr.slb.com) Schlumberger-Doll Research