template<typename TYPE>
class density::lifo_array< TYPE >
Array container that allocates its elements from the thread-local data stack, and owns it. The block is deallocated by the destructor. This class should be used only on the automatic storage.
- Template Parameters
-
The data stack is a pool in which a thread can allocate and deallocate memory in LIFO order. It is handled by an internal lifo_allocator, which in turn in built upon an instance of default_allocator. lifo_array and lifo_buffer allocate on the data stack, so they must respect the LIFO order: only the most recently allocated block can be deallocated or reallocated. Instantiating lifo_array and lifo_buffer on the automatic storage (locally in a function) is always safe, as the language guarantees destruction in reverse order, even inside arrays or aggregate types.
Exactly like raw arrays and std::array
, the default constructor of lifo_array default-constructs the elements. So if elements have POD type, they are not initialized. In contrast, std::vector
value-initializes its elements, so POD types are initialized to zero.
lifo_array<double> numbers(7);
for (auto & num : numbers)
num = 1.;
auto const sum = std::accumulate(numbers.begin(), numbers.end(), 0.);
assert(sum == 7.);
lifo_array<double> other_numbers(7, 1.);
auto const other_sum = std::accumulate(other_numbers.begin(), other_numbers.end(), 0.);
assert(other_sum == 7.);
lifo_array<std::string> strings(10);
bool all_empty =
std::all_of(strings.begin(), strings.end(), [](const std::string & i_str) {
return i_str.empty();
});
assert(all_empty);
The size of a lifo_array is fixed at construction time and is immutable. Elements are constructed in positional order and destroyed in reverse order.
void concat_and_print(const char * i_str_1, const char * i_str_2)
{
auto const len_1 = strlen(i_str_1);
auto const len_2 = strlen(i_str_2);
memcpy(
string.
data(), i_str_1, len_1);
memcpy(
string.
data() + len_1, i_str_2, len_2);
string[len_1 + len_2] = 0;
std::cout << string.data() << std::endl;
}