invalidating objects in free store



 DEVELOP > c-Plus-Plus > invalidating objects in free store

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Thomas Maier-Komor"
Date: 01 Aug 2005 04:21:47 AM
Object: invalidating objects in free store
Hello everybody,
can anybody comment, if this code is standard conforming?
What I want is a mechanism to mark certain objects
on the heap as invalid, but I don't want to add a flag
"valid" to each class, which I have to check in every method...
What I am concerned about if this kind of code might cause
heap curroption...
TIA,
Tom
#include <cassert>
#include <new>
struct ABC
{
virtual ~ABC() = 0;
virtual bool valid() const
{ return true; }
};
inline ABC::~ABC()
{ }
struct Int : public ABC
{
Int(int a = 0)
: ABC()
, value(a)
{ }
int getValue() const
{ return value; }
private:
int value;
};
struct Invalid : public ABC
{
Invalid()
: ABC()
{ }
bool valid() const
{ return false; }
};
int main()
{
assert(sizeof(Invalid) <= sizeof(Int));
ABC *abc = new Int();
new (abc) Invalid();
delete abc;
abc = 0;
return 0;
}
.

User: "Gianni Mariani"

Title: Re: invalidating objects in free store 01 Aug 2005 05:38:00 AM
Thomas Maier-Komor wrote:

Hello everybody,

can anybody comment, if this code is standard conforming?
What I want is a mechanism to mark certain objects
on the heap as invalid, but I don't want to add a flag
"valid" to each class, which I have to check in every method...

What I am concerned about if this kind of code might cause
heap curroption...

....

int main()
{
assert(sizeof(Invalid) <= sizeof(Int));

ABC *abc = new Int();
new (abc) Invalid();
delete abc;
abc = 0;
return 0;
}

This is an error according to the standard. The behaviour is undefined.
It may work like you think it does or your CPU might melt and anything
in between. Don't count on it working on all platforms.
.
User: "Thomas Maier-Komor"

Title: Re: invalidating objects in free store 01 Aug 2005 06:22:31 AM
Gianni Mariani wrote:

Thomas Maier-Komor wrote:

Hello everybody,

can anybody comment, if this code is standard conforming?
What I want is a mechanism to mark certain objects
on the heap as invalid, but I don't want to add a flag
"valid" to each class, which I have to check in every method...

What I am concerned about if this kind of code might cause
heap curroption...

...

int main()
{
assert(sizeof(Invalid) <= sizeof(Int));

ABC *abc = new Int();
new (abc) Invalid();
delete abc;
abc = 0;
return 0;
}



This is an error according to the standard. The behaviour is undefined.
It may work like you think it does or your CPU might melt and anything
in between. Don't count on it working on all platforms.

could you please tell me where I find this in the standard. At what
point do I trigger UB (I suppose the placement new)?
TIA,
Tom
.
User: "msalters"

Title: Re: invalidating objects in free store 02 Aug 2005 06:01:41 AM
Thomas Maier-Komor schreef:

int main()
{
assert(sizeof(Invalid) <= sizeof(Int));

ABC *abc = new Int();
new (abc) Invalid();
delete abc;
abc = 0;
return 0;
}

At what point do I trigger UB (I suppose the placement new)?

Indeed. If you had exchanged the delete and the placement new, it
would be OK. After the delete, abc points to uninitialized memory
that is properly sized (assuming the assertion passed).
Effectively, you're using a vtable (or its equivalent) to store
your invalid bit.
HTH,
Michiel Salters
.



User: "Joe Seigh"

Title: Re: invalidating objects in free store 01 Aug 2005 05:28:23 AM
Thomas Maier-Komor wrote:

Hello everybody,

can anybody comment, if this code is standard conforming?
What I want is a mechanism to mark certain objects
on the heap as invalid, but I don't want to add a flag
"valid" to each class, which I have to check in every method...

What I am concerned about if this kind of code might cause
heap curroption...

You'd have to add a flag or bit of some sort unless you
want to violate information theory. *Every* object would
require it since "valid" implies a "not valid". You'd be
better off using a custom heap manager as the easiest way
to get the bit into the base object. But you'd need to
be careful since not all objects are required to be on the
heap.
Before you go to all this trouble, you might want to google
smart pointers first. Other terms are GC (garbage collection)
and reference counting. Also Boost's shared_ptr.
--
Joe Seigh
When you get lemons, you make lemonade.
When you get hardware, you make software.
.
User: "Thomas Maier-Komor"

Title: Re: invalidating objects in free store 01 Aug 2005 06:28:27 AM
Joe Seigh wrote:

Thomas Maier-Komor wrote:

Hello everybody,

can anybody comment, if this code is standard conforming?
What I want is a mechanism to mark certain objects
on the heap as invalid, but I don't want to add a flag
"valid" to each class, which I have to check in every method...

What I am concerned about if this kind of code might cause
heap curroption...


You'd have to add a flag or bit of some sort unless you
want to violate information theory. *Every* object would
require it since "valid" implies a "not valid". You'd be
better off using a custom heap manager as the easiest way
to get the bit into the base object. But you'd need to
be careful since not all objects are required to be on the
heap.

Before you go to all this trouble, you might want to google
smart pointers first. Other terms are GC (garbage collection)
and reference counting. Also Boost's shared_ptr.

you are missing my point - I don't want to do garbage collection.
I might have references to objects, which have become by their
very nature meaningless. This does not mean that there may
not be a valid references to any such object at runtime. So I
would like them to throw an apriopriate exception, if any of
their methods is invoked.
The point is that I don't want to implement this feature for
every method in every class, but I would prefer to implement it
just once and get a consistent behaviour across all classes.
In consequence an implementation in a common base class
is not the way to go...
Any other ideas?
TIA,
Tom
.
User: "Joe Seigh"

Title: Re: invalidating objects in free store 01 Aug 2005 11:02:30 AM
Thomas Maier-Komor wrote:

Joe Seigh wrote:

Thomas Maier-Komor wrote:

Hello everybody,

can anybody comment, if this code is standard conforming?
What I want is a mechanism to mark certain objects
on the heap as invalid, but I don't want to add a flag
"valid" to each class, which I have to check in every method...

What I am concerned about if this kind of code might cause
heap curroption...


You'd have to add a flag or bit of some sort unless you
want to violate information theory. *Every* object would
require it since "valid" implies a "not valid". You'd be
better off using a custom heap manager as the easiest way
to get the bit into the base object. But you'd need to
be careful since not all objects are required to be on the
heap.

Before you go to all this trouble, you might want to google
smart pointers first. Other terms are GC (garbage collection)
and reference counting. Also Boost's shared_ptr.


you are missing my point - I don't want to do garbage collection.

I might have references to objects, which have become by their
very nature meaningless. This does not mean that there may
not be a valid references to any such object at runtime. So I
would like them to throw an apriopriate exception, if any of
their methods is invoked.

The point is that I don't want to implement this feature for
every method in every class, but I would prefer to implement it
just once and get a consistent behaviour across all classes.
In consequence an implementation in a common base class
is not the way to go...

Any other ideas?

Something like weak_ptr in conjunction with shared_ptr to manage
object validity. If you have an objection to the term "smart
pointer", you might be able to get someone to rewrite them and
change the terminology to something else. That way you can
pretend to be using something different.
--
Joe Seigh
When you get lemons, you make lemonade.
When you get hardware, you make software.
.
User: "Thomas Maier-Komor"

Title: Re: invalidating objects in free store 01 Aug 2005 11:46:09 AM
Joe Seigh wrote:

Thomas Maier-Komor wrote:

Joe Seigh wrote:

Thomas Maier-Komor wrote:

Hello everybody,

can anybody comment, if this code is standard conforming?
What I want is a mechanism to mark certain objects
on the heap as invalid, but I don't want to add a flag
"valid" to each class, which I have to check in every method...

What I am concerned about if this kind of code might cause
heap curroption...


You'd have to add a flag or bit of some sort unless you
want to violate information theory. *Every* object would
require it since "valid" implies a "not valid". You'd be
better off using a custom heap manager as the easiest way
to get the bit into the base object. But you'd need to
be careful since not all objects are required to be on the
heap.

Before you go to all this trouble, you might want to google
smart pointers first. Other terms are GC (garbage collection)
and reference counting. Also Boost's shared_ptr.


you are missing my point - I don't want to do garbage collection.

I might have references to objects, which have become by their
very nature meaningless. This does not mean that there may
not be a valid references to any such object at runtime. So I
would like them to throw an apriopriate exception, if any of
their methods is invoked.

The point is that I don't want to implement this feature for
every method in every class, but I would prefer to implement it
just once and get a consistent behaviour across all classes.
In consequence an implementation in a common base class
is not the way to go...

Any other ideas?

Something like weak_ptr in conjunction with shared_ptr to manage
object validity. If you have an objection to the term "smart
pointer", you might be able to get someone to rewrite them and
change the terminology to something else. That way you can
pretend to be using something different.


I don't have any objection against shared or smart or whatever
special pointer classes. The point is that they don't solve
the problem of changing the state of all virtual functions
to a specific semantics without huge implementation effort...
Tom
.
User: "Joe Seigh"

Title: Re: invalidating objects in free store 01 Aug 2005 12:04:39 PM
Thomas Maier-Komor wrote:



I don't have any objection against shared or smart or whatever
special pointer classes. The point is that they don't solve
the problem of changing the state of all virtual functions
to a specific semantics without huge implementation effort...

You don't need to use the reference counting part if you don't
want to. Just make non intrusive smart ptr that checks state
in the pointer meta data when it's dereferenced. A non intrusive
smart pointer is the easiest way of wrapping access to objects
without modifying the object.
--
Joe Seigh
When you get lemons, you make lemonade.
When you get hardware, you make software.
.
User: "Thomas Maier-Komor"

Title: Re: invalidating objects in free store 02 Aug 2005 04:34:04 AM
Joe Seigh wrote:

Thomas Maier-Komor wrote:



I don't have any objection against shared or smart or whatever
special pointer classes. The point is that they don't solve
the problem of changing the state of all virtual functions
to a specific semantics without huge implementation effort...


You don't need to use the reference counting part if you don't
want to. Just make non intrusive smart ptr that checks state
in the pointer meta data when it's dereferenced. A non intrusive
smart pointer is the easiest way of wrapping access to objects
without modifying the object.

Yes that's sounds like the standard implementation of the state
pattern of Gamma's book. This is definitly a good solution.
Thanks,
Tom
.







  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER