Thread View: comp.std.c++
4 messages
4 total messages
Started by Heeren Pathak
Thu, 29 Oct 1992 16:18
New/Constructor & Destructor/Delete Relationship
Author: Heeren Pathak
Date: Thu, 29 Oct 1992 16:18
Date: Thu, 29 Oct 1992 16:18
56 lines
1615 bytes
1615 bytes
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
Re: New/Constructor & Destructor/Delete Relationship
Author: Heeren Pathak
Date: Fri, 30 Oct 1992 18:32
Date: Fri, 30 Oct 1992 18:32
13 lines
825 bytes
825 bytes
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
Re: New/Constructor & Destructor/Delete Relationship
Author: Jim Adcock
Date: Tue, 03 Nov 1992 19:36
Date: Tue, 03 Nov 1992 19:36
14 lines
1083 bytes
1083 bytes
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?
Re: New/Constructor & Destructor/Delete Relationship
Author: John MAX Skaller
Date: Wed, 04 Nov 1992 06:45
Date: Wed, 04 Nov 1992 06:45
29 lines
1614 bytes
1614 bytes
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 ------------------
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