Topic: New/Constructor & Destructor/Delete Relationship


Author: jimad@microsoft.com (Jim Adcock)
Date: 03 Nov 92 19:36:06 GMT
Raw View
In article <pathak-301092142826@maclab2-e.mitre.org> pathak@mitre.org (Heeren Pathak) writes:
|I need to know if the language enforces the following:
|
|Is the constructor called immediately after the new operator?

No.  See in part $r.5.3.3: "The order of evaluation of the call to an
operator new() to get memory and the evaluation of arguments to
constructors is undefined"  For example if a parm to the ctor was
itself a new'ed object that new could happen after the new in question,
but before the ctor to whom this new'ed object is a parm.

|Is the delete operator called immediately after the destructor?

I can't find a reference to this issue.  Ill defined?





Author: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
Date: Wed, 4 Nov 1992 06:45:31 GMT
Raw View
In article <pathak-291092115943@maclab1-e.mitre.org> pathak@mitre.org (Heeren Pathak) writes:
>
>I have a situation where I need to know how an object was created
>(dynamically or on the stack) and have delete only delete dynamic objects.

 I wonder if the language could be extended to allow the storage
class of the thing pointed to by a pointer to be an attribute of the pointer.

 For example suppose we have:

 int heap* x;
 int global* y;
 int stack* z;

analogously to 'const' and 'volatile', then

 delete y; // compile time error
 delete z; // compile time error

 int heap& f(){ ... return *z;} // compile time error

We could then make linked lists of pointers to heap objects.
Mm. Doesnt stop dangling pointers though.

--
;----------------------------------------------------------------------
        JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
 Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------




Author: pathak@mitre.org (Heeren Pathak)
Date: Thu, 29 Oct 1992 16:18:51 GMT
Raw View
I have a situation where I need to know how an object was created
(dynamically or on the stack) and have delete only delete dynamic objects.
I came up with the following mechanism:

class Base {

public:
   new(...);
   Base(...);
   ~Base();
   delete(...);

   static int createdDynamically;

   int objectIsDynamic;
};

Base::createdDynamically = 0;

Base::new(...)
{
  Base::createdDynamically  = 1;
  // do other stuff;
}

Base::Base(...)
{
  this->objectIsDynamic = Base::createdDynamically;
  Base::createdDynamically  = 0;
  // do other stuff;
}

Base::~Base()
{
  Base::createdDynamically = this->objectIsDynamic;
  // Do other stuff
}

Base::delete(...)
{
  int isDynamic = Base::createdDynamically;
  Base::createdDynamically = 0;
  // do other stuff
}

Assuming single tasking machine.

This scheme should allow me to pass information between new and delete as
long as I am guarenteed that the constructor is called after the new.  I
haven't been able to find any reference to this in ARM so.... Does anyone
know the correct answer?


Heeren Pathak
pathak@mitre.org




Author: pathak@mitre.org (Heeren Pathak)
Date: Fri, 30 Oct 1992 18:32:13 GMT
Raw View
I reread my original posting and realized that I wasn't very clear so let
me try again...

I need to know if the language enforces the following:

Is the constructor called immediately after the new operator?

Is the delete operator called immediately after the destructor?


Heeren Pathak
pathak@mitre.org