Boost logo

Boost Users :

From: Gaier, Bjoern (Bjoern.Gaier_at_[hidden])
Date: 2020-10-22 05:49:10


Oh gosh.... how could I oversee THAT? >o<

Allright... now it works and I understand the difference.... Thank you >o<

-----Original Message-----
From: i.nixman_at_[hidden] <i.nixman_at_[hidden]>
Sent: 21 October 2020 16:55
To: boost-users_at_[hidden]
Cc: Gaier, Bjoern <Bjoern.Gaier_at_[hidden]>
Subject: Re: [Boost-users] [Newbie][Intrusive] - Beginners question

On 2020-10-21 14:33, Gaier, Bjoern via Boost-users wrote:
> 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]);
> }
>



because `test` has 5 elems:

> 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 mailing list
> Boost-users_at_[hidden]
> https://lists.boost.org/mailman/listinfo.cgi/boost-users
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