[Newbie][Intrusive] - Beginners question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[Newbie][Intrusive] - Beginners question

Boost - Users mailing list

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 mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Newbie][Intrusive] - Beginners question

Boost - Users mailing list
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
> [hidden email]
> https://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Newbie][Intrusive] - Beginners question

Boost - Users mailing list
Oh gosh.... how could I oversee THAT? >o<

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

-----Original Message-----
From: [hidden email] <[hidden email]>
Sent: 21 October 2020 16:55
To: [hidden email]
Cc: Gaier, Bjoern <[hidden email]>
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
> [hidden email]
> 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 mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users