Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!usc!elroy.jpl.nasa.gov!decwrl!ucbvax!mtxinu!unisoft!hoptoad!hsfmsh!hsfmsh.uucp!mhyman From: mhyman@hsfmsh.uucp (Marco S. Hyman) Newsgroups: comp.lang.c++ Subject: Re: Questions about new and array definitions Message-ID: <2486@hsfmsh.UUCP> Date: 8 Mar 90 22:14:50 GMT Sender: mhyman@hsfmsh.UUCP Reply-To: marc@dumbcat.UUCP (Marco S. Hyman) Organization: SoftCom, Inc. San Francisco Lines: 88 There are some differences between Zortech and cfront in the use of new with array definitions. This, from a co-worker [[with my comments like this]], shows the differences. Which is right? ---------------------------- I saw your message on USENET. Something didn't seem exactly the same as what I did, so I ran your examples through the Zortech compiler. As I suspected, the results I got were different (but not substantially, except in first case) from those with CFront: // Base class definition -- doesn't change. class F { void * (*a)[]; public: F( int size ); }; First constructor compiles, but generates the wrong code. Allocates one void *, then tries to dereference with [size]. Odd that Cfront didn't like this one at all: F::F( int size ) { a = new (void *)[size]; // case 1 } Second constructor doesn't compile: F::F( int size ) { a = new void *(*)[size]; // case 2 ^ "test2.cpp", line 13 Syntax error: expression expected } Third doesn't compile: F::F( int size ) { a = new (void *(*)[size]); // case 3 ^ "test3.cpp", line 13 Syntax error: integer constant expression expected } Forth constructor. This works! Without changing the base definition of the array a! I think it is because a[] and (*a)[] are considered identical to C. (consider that an array is known by the pointer to its first element!) F::F( int size ) { a = new void **[size]; // case 4 } [[This does not work with cfront unless the definition of a is changed to void ***a; cfront give a type match error]] Fifth constructor. Compiles, but generates the same code as case 1. F::F( int size ) { a = new (void *(*)[])[size]; // case 5 } There were other variations I tried in a vain attempt to get this business to work, but I can't remember them all. Suffice it to say that the designers of C++ sure made the good old process of a = malloc( size * sizeof(void *) ) and made it much too obfuscated in an attempt to make it type-safe. You can post this, if you like. Bill Coleman ------------------------------------- If you'd like to respond to Bill use one of the following addresses. hsfmsh!bcoleman-vax@sfsun.west.sun.com ...!hoptoad!hsfmsh!bcoleman-vax // marc