[gnu.g++.bug] Variable sized objects

keffer@SPERM.OCEAN.WASHINGTON.EDU (Tom Keffer) (11/11/89)

Michael,
        I am trying to figure out how to use g++ to make objects of
variable size.  This may reflect a misunderstanding on my part about
how overloaded new with extra arguments is supposed to work (no clues
in Lippman about this).

As always, Sun 3/260, SunOS 3.5.

g++ 1.36.1 w. libg++ 1.36.1

---
 Dr. Thomas Keffer          | Internet: keffer@ocean.washington.edu
 Rogue Wave Assoc.          | BITNET:   keffer%ocean.washington.edu@UWAVM
 Seattle, WA 98145          | uucp:     uw-beaver!ocean.washington.edu!keffer
 (206) 523-5831             | Telemail: T.KEFFER/OMNET

#include <stdlib.h>

class Vector {
  int nelem;	//Number of elements
  int array[1];	//Placeholder for beginning of data space
public:
  // Allocate enuf space for a Vector of length "n" off the heap.
  // Is this syntax correct?
  void*	operator new(long size, int n);
  Vector(int n) {nelem = n;}
};

main()
{
  // Should make a vector capable of holding 44 elements.  I think.
  Vector *a = new (44) Vector(44);
}

void*
Vector::operator new(long size, int n)
{
  void* hold = malloc(size + (n-1)*sizeof(int));
  return hold;
}

Results:
g++ -v junk.cc
gcc version 1.36.1 (based on GCC 1.36)
 /usr/local/lib/gcc-cpp -+ -v -undef -D__GNUC__ -D__GNUG__ -D__cplusplus -Dmc68000 -Dsun -Dunix -D__mc68000__ -D__sun__ -D__unix__ -D__HAVE_68881__ -Dmc68020 junk.cc /usr/tmp/cca22262.cpp
GNU CPP version 1.36
 /usr/local/lib/gcc-cc1plus /usr/tmp/cca22262.cpp -quiet -dumpbase junk.cc -version -o /usr/tmp/cca22262.s
GNU C++ version 1.36.1 (based on GCC 1.36) (68k, MIT syntax) compiled by GNU C version 1.36.
default target switches: -m68020 -mc68020 -m68881 -mbitfield
junk.cc: In method Vector::Vector (int):
junk.cc:10: too few arguments for method `operator new'
junk.cc: In function int main ():
junk.cc:16: parse error before `44'

Compilation exited abnormally with code 1 at Fri Nov 10 13:27:07

dl@G.OSWEGO.EDU (Doug Lea) (11/15/89)

You CAN'T make variable-sized objects if they are ever to be local
(auto or static). There's just no way to do it, since in C++, the
sizeof() any local must be knowable at compile time. You have to make
the variable-sized thing live on the freestore, and have the
`externally visible' class just a pointer to it (and also manually
manage its allocation/deallocation). Libg++ Strings and a few other
classes do it this way.  Write me if you'd like help with the details.

-Doug

keffer@SPERM.OCEAN.WASHINGTON.EDU (Tom Keffer) (11/16/89)

dl> Date: Wed, 15 Nov 89 06:42:25 EST
dl> From: dl@g.oswego.edu (Doug Lea)
dl> Reply-To: dl@oswego.oswego.edu


dl> You CAN'T make variable-sized objects if they are ever to be local
dl> (auto or static). There's just no way to do it, since in C++, the
cl> sizeof() any local must be knowable at compile time. You have to make
dl> the variable-sized thing live on the freestore, and have the
dl> `externally visible' class just a pointer to it (and also manually
dl> manage its allocation/deallocation). Libg++ Strings and a few other
dl> classes do it this way.  Write me if you'd like help with the details.

dl> -Doug

This is, of course, the way I am doing it (even I know THAT ! :-).
But, it seems that I can't even do it on the freestore unless I assign
to "this", a la Stroustrup, p. 165, and that ain't allowed any more
with plain vanilla g++ 1.36 (you must specify the -fthis-is-variable
flag and take the resultant performance hit).

-tk

---
 Dr. Thomas Keffer          | Internet: keffer@ocean.washington.edu
 Rogue Wave Assoc.          | BITNET:   keffer%ocean.washington.edu@UWAVM
 Seattle, WA 98145          | uucp:     uw-beaver!ocean.washington.edu!keffer
 (206) 523-5831             | Telemail: T.KEFFER/OMNET