Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

Examples

Aligned allocation

To dynamically allocate storage with desired alignment, you can use the aligned_alloc function:

  1. void* storage = boost::alignment::aligned_alloc(alignment, size);

To deallocate storage allocated with the aligned_alloc function, use the aligned_free function:

  1. boost::alignment::aligned_free(storage);

Aligned allocator

For C++ allocator aware code, you can use the aligned_allocator class template for an allocator that respects over-alignment:

  1. std::vector<int128_t, boost::alignment::aligned_allocator<int128_t> > vector;

This template allows specifying minimum alignment for all dynamic allocations:

  1. std::vector<double, boost::alignment::aligned_allocator<double, 64> > vector;

Aligned allocator adaptor

To turn an allocator into an allocator that respects over-alignment, you can use the aligned_allocator_adaptor class template:

  1. boost::alignment::aligned_allocator_adaptor<First> second(first);

This template allows specifying minimum alignment for all dynamic allocations:

  1. boost::alignment::aligned_allocator_adaptor<First, 64> second(first);

Aligned deleter

For a deleter that can be paired with aligned_alloc, you can use the aligned_delete class:

  1. std::unique_ptr<double, boost::alignment::aligned_delete> pointer;

Pointer alignment

To advance a pointer to the next address with the desired alignment:

  1. void* pointer = storage;
  2. std::size_t space = size;
  3. void* result = boost::alignment::align(64, sizeof(double), pointer, space);

Querying alignment

To obtain the alignment of a given type at compie time, you can use:

  1. boost::alignment::alignment_of<int128_t>::value

If your compiler supports C++14 variable templates, you can also use:

  1. boost::alignment::alignment_of_v<int128_t>

Hinting alignment

To inform the compiler about the alignment of a pointer, you can use:

  1. BOOST_ALIGN_ASSUME_ALIGNED(pointer, 64)

Checking alignment

To check alignment of a pointer you can use the is_aligned function:

  1. assert(boost::alignment::is_aligned(pointer, 64));

PrevUpHomeNext