Boost logo

Boost Users :

From: Gaier, Bjoern (Bjoern.Gaier_at_[hidden])
Date: 2020-10-21 14:33:25


Hey everybody,

I looked into the boost intrusive container and played around with it a bit. I tried the following code:
struct animal : public boost::intrusive::list_base_hook<>
{
                std::string name;
                int legs;

                animal(std::string n, int l) : name{std::move(n)}, legs{l} {}
};

int main(int argc, char **argv)
{
                std::vector<animal> test;
                boost::intrusive::list<animal> wuff;

                for(int n = 0; n < 5; n++)
                {
                               test.push_back(animal("Katze", 4));
                }

                for(int n = 0; n < 5; n++)
                {
                               wuff.push_back(test[n]);
                }

                for(int n = 0; n < 15; n++)
                {
                               wuff.push_back(test[n]);
                }

                for(auto elem : wuff)
                {
                               printf("%s\n", elem.name.c_str());
                }
}

This code does crash! I guess this is because "test" relocated the memory and therefor "wuff" lost its references. I would expect the same result if I would have a std::list with pointers to animal structures - this is why I wonder what the benefit of an intrusive container is compared to the same container with just pointers in it.

Hope someone can explain me this! Thank you in advance and kind greetings
Björn
Als GmbH eingetragen im Handelsregister Bad Homburg v.d.H. HRB 9816, USt.ID-Nr. DE 114 165 789 Geschäftsführer: Dr. Hiroshi Nakamura, Dr. Robert Plank, Markus Bode, Heiko Lampert, Takashi Nagano, Junichi Tajika, Ergin Cansiz.



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