Boost logo

Boost Users :

From: Bernhard Maeder (bernhard.maeder_at_[hidden])
Date: 2008-05-22 13:22:18


Robert Ramey <ramey <at> rrsd.com> writes:

>
> This can't work.
>
> load a pointer creates a new pointer with a copy of the original object.
>
> adding the following to your test should make this clear
>
> ...
> std::cout << "ref: " << *m.ref << std::endl;
> ...
> std::cout << "ref: " << *m2.ref << std::endl;
>

Hmm, if I understand you correctly, then m2.ref should point to a copy of m.m[1]
then, but not the same instance? The thing is, m2.ref isn't even that.

Here's an enhanced example: I add a value variable to my dummy struct:

struct dummy
{
    int j;
    dummy() : j(-1) {}
    dummy(int i) : j(i) {}
    template<typename Archive> void serialize(Archive & ar, const unsigned int
version)
    {
        ar & j;
    }
};

And then print that value along with the addresses:

    ...
    std::cout << "0: " << &m.m[0] << " = " << m.m[0].j << std::endl;
    std::cout << "1: " << &m.m[1] << " = " << m.m[1].j << std::endl;
    std::cout << "2: " << &m.m[2] << " = " << m.m[2].j << std::endl;
    std::cout << "ref: " << m.ref << " = " << m.ref->j << std::endl;
    ...

What I get is the following:

0: 0x8085c0c = 1
1: 0x8085c44 = 2
2: 0x8085c7c = 3
ref: 0x8085c44 = 2

0: 0x80864a4 = 1
1: 0x80864c4 = 2
2: 0x80864e4 = 3
ref: 0xbf81de0c = -1231556796

which indicates to me that it isn't a copy of the original element and that
something unexpected is happening.

As I understand Boost.Serialization's mechanics, one is allowed to store objects
through pointers that are stored by-value elsewhere, as long as the by-value
serialization is done before to the by-pointer one. I have some code where this
(seemingly) works. For the above, e.g., replacing the map by a vector<dummy>
makes everything work as expected:

0: 0x8082440 = 1
1: 0x8082444 = 2
2: 0x8082448 = 3
ref: 0x8082444 = 2

0: 0x8083368 = 1
1: 0x808336c = 2
2: 0x8083370 = 3
ref: 0x808336c = 2

Coincidince?

Anyway, what would be the best way of doing this? Keeping pointers (instead of
values) to dummy in the map should certainly fix the problem, right?

Thanks!
Bernhard


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net