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 for the latest Boost documentation.
PrevUpHomeNext

Tutorial

Using align
Using aligned_alloc
Using aligned_allocator
Using aligned_allocator_adaptor
Using aligned_delete
Using alignment_of
Using is_aligned

We want to default-construct an object of a potentially over-aligned generic type, T, in storage of size n, whose address is stored in a pointer, p.

#include <boost/align/align.hpp>

Use the align function to adjust the pointer so that it is suitably aligned.

auto result = boost::alignment::
    align(alignof(T), sizeof(T), p, n);

If successful, n is decreased by the byte count that p was advanced to be suitably aligned, and the adjusted value of p is returned. It now points to an address at which to construct an object of type T.

if (result) {
    ::new(result) T();
}

If unsuccessful, because n has insufficient space to fit an object of the requested size after adjusting p to have the requested alignment, a null pointer is returned and p and n are not changed.

else {
    throw std::exception();
}

We want to dynamically allocate storage for, and default-construct within that storage, an object of a generic type T that is potentially over-aligned.

#include <boost/align/aligned_alloc.hpp>

Allocate an object of a desired alignment and size using the aligned_alloc allocation function.

auto p = boost::alignment::
    aligned_alloc(alignof(T), sizeof(T));

If successful, a non-null pointer is returned. To free this storage the aligned_free function is used.

if (p) {
    try {
        ::new(p) T();
    } catch (...) {
        boost::alignment::aligned_free(p);
        throw;
    }
}

If unsuccessful, a null pointer is returned.

else {
    throw std::bad_alloc();
}

Free this storage, via the aligned_free function, when it is no longer required.

boost::alignment::aligned_free(p);

We want to use standard library allocator-aware containers, such as vector, with a generic type, T, that is potentially over-aligned.

#include <boost/align/aligned_allocator.hpp>

Specify the aligned_allocator allocator via the container's allocator template parameter.

std::vector<T, boost::alignment::
    aligned_allocator<T> > v;

If we wanted a vector of a different type, such as int, but desired that each integer object had the alignment of T, this is possible by specifying the minimum alignment with the allocator.

std::vector<int, boost::alignment::
    aligned_allocator<int, alignof(T)> > v;

We want to make an existing allocator type, A, alignment-aware and use it with a standard library container, such as vector, with a type, T, that is potentially over-aligned.

#include <boost/align/aligned_allocator_adaptor.hpp>

We use class template aligned_allocator_adaptor as the vector's allocator type.

std::vector<T, boost::alignment::
    aligned_allocator_adaptor<A> > v(a);

If we wanted a vector of a different type, such as int, but desired that each integer object had the alignment of T, this is possible by specifying the minimum alignment with the allocator adaptor.

std::vector<int, boost::alignment::
    aligned_allocator_adaptor<A, alignof(T)> > v(a);

We want a default deleter for use with unique_ptr that can be used to deallocate and destroy objects which were constructed in storage that was allocated with aligned_alloc.

#include <boost/align/aligned_delete.hpp>

Storage is allocated for the object using aligned_alloc.

auto p = boost::alignment::
    aligned_alloc(alignof(T), sizeof(T));
if (!p) {
    throw std::bad_alloc();
}

An object is constructed in that storage using placement new.

try {
    q = ::new(p) T();
} catch (...) {
    boost::alignment::aligned_free(p);
    throw;
}

Use the aligned_delete class as the deleter template parameter.

std::unique_ptr<T,
    boost::alignment::aligned_delete> u(q);

We want to assert at compile time that the alignment of a type, T, is at least as large as the alignment of a type, U.

#include <boost/align/alignment_of.hpp>

Use the alignment_of class template to determine the alignment of the type.

static_assert(boost::alignment::alignment_of<T>::
    value >= boost::alignment::alignment_of<U>::
    value, "");

We want to assert that the alignment of a pointer, p, is at least the alignment of a generic type, T, that is potentially over-aligned.

#include <boost/align/is_aligned.hpp>

Use the is_aligned function to test the alignment of the pointer.

assert(boost::alignment::is_aligned(alignof(T), p));


PrevUpHomeNext