🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by Tom Keffer Fri, 10 Nov 1989 21:30
Variable sized objects
#569
Author: Tom Keffer
Date: Fri, 10 Nov 1989 21:30
55 lines
2338 bytes
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
Variable sized objects
#587
Author: Doug Lea
Date: Wed, 15 Nov 1989 11:42
10 lines
947 bytes
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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads