[xsd-users] how long is a pointer still valid when working with the data object model?

Boris Kolpackov boris at codesynthesis.com
Mon Jan 31 10:22:02 EST 2011


Hi Erik,

Erik Sjölund <erik.sjolund at gmail.com> writes:

> After adding an object with the help of
> 
>  void  push_back (std::auto_ptr<X>)
> 
> to a sequence class, is it afterwards still possible to use a pointer
> of the added object that you might have kept?
> I guess the auto_ptr indicates you should not really be doing this.

Yes, you can "cache" the raw pointer and then use it to reference the
object. You should be careful, however, to make sure the object is
still alive (see below).


> I have also been wondering how long you may assume that a pointer is
> still valid after doing something like
> &(someElementName().[index])

It is valid for as long as the object model is valid and the sequence
to which this element belongs to is not cleared, etc. In essence, not-
fundamental object model types are stored in sequences as pointers to
the dynamically-allocated instances. You can view such a sequence as
std::vector<some_smart_ptr<T>> for object lifetime purposes.


> For instance if you know that you have only added things with
> push_back() to a sequence class, can you then assume that pointers to
> previous "siblings" are unchanged?

Yes, for non-fundamental C++ types that is true.

Boris



More information about the xsd-users mailing list