[Pool] Use singleton_pool in struct
Hi!
I have a problem with singleton_pool when I use it into a struct.
I think that this problem is my bad knowledge of C++ and not a problem of
boost pool.
#include <iostream>
#include
Hi Claude,
Since the first usage compiles fine I guess "malloc" is a static method of
boost::singleton_pool
Try changing your call to
myStruct::poolMemory::malloc
Regards,
koval
2012/10/8 Claude
Hi! I have a problem with singleton_pool when I use it into a struct. I think that this problem is my bad knowledge of C++ and not a problem of boost pool.
#include <iostream> #include
using namespace std;
struct data { int a,b; char name[50]; };
struct MyOrderTag {}; typedef boost::singleton_pool
poolMemory; struct myStruct { struct MyOrderTag {}; typedef boost::singleton_pool
poolMemory; }; int main() { data *d = (data*)poolMemory::malloc(); //This is ok
myStruct ms; // data *d1 = (data*)ms.poolMemory::malloc(); //Error!
d->a = 2;
poolMemory::release_memory(); return 0; }
I obtain a compiler error on row: data *d1 = (data*)ms.poolMemory::malloc(); The poolMemory in struct is only a typedef and not a struct field. How can I use a singleton_pool into a struct?
-- View this message in context: http://boost.2283326.n4.nabble.com/Pool-Use-singleton-pool-in-struct-tp46367... Sent from the Boost - Users mailing list archive at Nabble.com. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Ok, now it work well, thanks!
But..I have another problem. In this code (C++11 with std::tuple)
#include <iostream>
#include <map>
#include <tuple>
#include
I am just guessing, as I have never used singleton_pool
It looks like malloc() returns raw memory. You just cast the raw pointer to
data*
Since 'data' is a non-POD type it should be initialized by calling the
constructor
I would suggest changing the call to
data* d = new(myStruct::poolMemory::malloc()) data();
This placement new syntax results in calling the constructor on the raw
address obtained from malloc()
The default constructor will initialize "myComplexType" and make it usable
Hope that helps
2012/10/12 Claude
Ok, now it work well, thanks!
But..I have another problem. In this code (C++11 with std::tuple)
#include <iostream> #include <map> #include <tuple> #include
using namespace std;
struct data { int a,b; char name[50]; std::mapstd::string,std::tuple<uint16_t,uint16_t> myComplexType; };
struct myStruct { struct MyOrderTag {}; typedef boost::singleton_pool
poolMemory; }; int main() { data *d = (data*)myStruct::poolMemory::malloc();
d->a = 8; strcpy(d->name,"Hello!");
//fill myComplexType field (this row causes a segfault!) d->myComplexType["one"] = std::tuple
(1,2); myStruct::poolMemory::release_memory(); return 0; }
I obtain a segmentation fault error when assign a tuple at myComplexType field. What is wrong?
-- View this message in context: http://boost.2283326.n4.nabble.com/Pool-Use-singleton-pool-in-struct-tp46367... Sent from the Boost - Users mailing list archive at Nabble.com. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Claude
-
Piotr Kowalski