home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: Undefined new and delete
- Message-ID: <1992Dec22.173713.10373@taumet.com>
- Organization: TauMetric Corporation
- References: <1992Dec21.162241.12012@cbnewsk.cb.att.com>
- Date: Tue, 22 Dec 1992 17:37:13 GMT
- Lines: 36
-
- swk7@cbnewsk.cb.att.com (steven.w.koenig) writes:
-
- >If one wanted to insure that no objects of a given type were ever created
- >on the heap, it seems logical that one should be able to declare the
- >new and delete operators as private but not define them. cfront 2.1
- >only seems to support this if the constructors are inline. The following
- >example works if A and ~A are inline, but
- [ the linker complains that the new and delete are undefined ]
- >if A and ~A are not inline.
-
- Unfortunately, making the new/delete private won't entirely prevent
- objects from being constructed on the heap, since
- A *p = new A[10];
- will access the global operator new, not the class-specific version.
- (In the future, C++ will allow class-specific versions of the array
- allocation versions of new and delete.)
-
- To address your question, the functions show up as undefined due to a
- common constructor/destructor implementation technique.
-
- Typically, a flag is passed to the constructor (destructor) to indicate
- whether the object needs to be allocated on (returned to) the heap.
- The compiler generates code to test the flag and call operator new
- (operator delete). When the constructor/destructor is not inline,
- the compiler can't detect that the calls to new/delete are not needed,
- so they remain in the code. (The may have to generate quite a lot
- of hidden code in a constructor/destructor, and this is only part of it.)
-
- This implementation technique is allowed by the ARM, and I don't
- forsee anything in the emerging Standard to disallow it. You can
- define the functions as inline calls to abort() or assert() to get
- around the problem. It defers detection to run time instead of
- link time, though.
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
-