2017-01-25 37 views
0

我嘗試使用boost :: simple_segregated_storage,但我不能理解如何正確使用它。沒有任何樣品。我在下一個方法使用它:如何使用boost :: simple_segregated_storage?

boost::simple_segregated_storage<int> pStorage; 

const int num_partitions = 100; 
const int partition_sz = sizeof(int); 
const int block_sz = partition_sz * num_partitions; 
int block[block_sz] = {}; 

pStorage.segregate(block, block_sz, partition_sz); 

int* pInt = (int*)pStorage.malloc(); // <-- Crashes here 

但我收到崩潰。我做錯了什麼,錯在哪裏? 如何正確使用它?

回答

2

您應該使用pStorage.add_block(block, block_sz, partition_sz);而不是segregate(),作爲segregate()只是爲了隔離塊成塊(我假定你知道塊與塊的概念,如果沒有,here是一個例證)。 add_block()block隔離,並將其自由列表合併到pStorage的免費列表中。在add_block()之後,pStorage不爲空,您可以從中分配內存。

相關問題