Why this code snippet encounter segment fault when setting the min_alloc_size for the constructor for boost::pool<> whereas it works well without setting the said parameter?
Here is the code snippet:
#include <boost/pool/pool.hpp>
#include <iostream>
int main()
{
boost::pool<> pool_a(1024*128);
boost::pool<> pool_b(1024*128, 5*1024*1024);
auto get_mem_blk = [](boost::pool<>& pool)
{
char* ptr = (char*)pool.ordered_malloc();
pool.ordered_free(ptr);
//memset(ptr, 0, 128 * 1024);
};
get_mem_blk(pool_a); //works well
std::cout << "pass first test" << std::endl;
get_mem_blk(pool_b); //segment fault!
std::cout << "pass second test" << std::endl;
}
Here is the output:
pass first test
bash: line 7: 8617 Segmentation fault (core dumped) ./a.out
Simply adding
-fsanitize=address,undefinedreveals:The problem seems to be that
next_sizemultiplies the partition size. Indeed, the default value is "only" 32. You probably meant the behaviour you get withor similar.
Live ON Coliru