Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!agate!helios.ee.lbl.gov!ux1.lbl.gov!beard
From: beard@ux1.lbl.gov (Patrick C Beard)
Newsgroups: comp.lang.c++
Subject: Re: use of new
Summary: new needs to know sizeof(class)
Message-ID: <3614@helios.ee.lbl.gov>
Date: 16 Aug 89 22:30:11 GMT
References: <2276@cadillac.CAD.MCC.COM>
Sender: usenet@helios.ee.lbl.gov
Reply-To: beard@ux1.lbl.gov (Patrick C Beard)
Organization: Lawrence Berkeley Laboratory, Berkeley
Lines: 36

In article <2276@cadillac.CAD.MCC.COM> vaughan@puma.cad.mcc.com (Paul Vaughan) writes:
>
>	Why is a complete class definition required for the use of the
>new operator?  Example:
>---------------------
>class foo;
>
>void bar() {
>  new foo;
>}
>---------------------
>produces the following error
>
>g++ -g -O -fchar-charconst -c -o trash.o trash.cc
>In function void bar ():
>trash.cc:4: invalid use of undefined type `struct foo'

The reason is that new has to know the size of the object being
allocated.  There is no way to determine this (as far as I know)
at run time.

To see why this is the case, here is the definition of new as an operator:

void* operator new(size_t size)
{
	return (void*)malloc(size);
}

So, if somebody comes up with a way to compute the size of an object at
runtime then we will be able to do what you are asking.


-------------------------------------------------------------------------------
-  Patrick Beard, Macintosh Programmer                        (beard@lbl.gov) -
-  Berkeley Systems, Inc.  "..............Good day!" - Paul Harvey  -
-------------------------------------------------------------------------------