Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!mailrus!ames!pacbell!hoptoad!hsfmsh!hsfmsh.uucp!mhyman From: mhyman@hsfmsh.uucp (Marco S. Hyman) Newsgroups: comp.lang.c++ Subject: Questions about new and array definitions Message-ID: <2473@hsfmsh.UUCP> Date: 7 Mar 90 20:19:14 GMT Sender: mhyman@hsfmsh.UUCP Reply-To: marc@dumbcat.UUCP (Marco S. Hyman) Organization: SoftCom, Inc. San Francisco Lines: 47 A co-worker came up with a problem that brings up an interesting C++ question. He wanted to create a pointer to an array of void pointers. The definition he used was: void * (*a)[]; In the constructor of the class `a' was defined in he tried to allocate memory using a = new (void *)[size]; // size was passed to the constructor Cfront complained with the error: error: bad assignment type: void *(*)[] = void * When the problem came to me I first tried some things to see if I could get memory allocated for `a' as defined. Things tried were a = new void *(*)[size]; error: syntax error a = new (void *(*)[size]); error: bad assignment type: void *(*)[] = void *(**)[size] a = new void **[size]; error: bad assignment type: void *(*)[] = void **[size] After several tries I came up with something cfront liked a = new (void *(*)[])[size]; The problem is that the code generated was not what was expected. Memory was allocated for one void pointer; size was not part of the allocation equation. The generated code looked like (cleaned up for easier reading): this->a = (((char *(**)[]) __nw__FUi(sizeof(char *(*)[]))))[size]; The solution was, of course, to get the [] out of the definition and use void ***a; a = new void **[size]; What is void * (*)[] then? Is there a way to allocate memory for it using new? // marc