I had used something like the following for this purpose:
#include <iostream>
#include <vector>
template<typename T>
class AlignedVector {
public:
AlignedVector() : data_(nullptr) {}
AlignedVector(int n)
: char_vec_(sizeof(T)*(n+1)),
data_(AlignedAddr(char_vec_.data())),
size_(n) {}
T* operator[](size_t n) { return data_[n]; }
const T* operator[](size_t n) const { return data_[n]; }
T* data() { return data_; }
const T* data() const { return data_; }
size_t size() const { return size_; }
void resize(size_t n) {
char_vec_.resize(sizeof(T)*(n+1));
data_ = AlignedAddr(char_vec_.data());
size_ = n;
}
private:
static T* AlignedAddr(char* addr) {
return (T*)(addr + sizeof(T) - ((size_t)addr % sizeof(T)));
}
std::vector<char> char_vec_;
T* data_;
size_t size_;
};
int main()
{
AlignedVector<int[128]> vec(13);
std::cout << (size_t)vec.data() << std::endl;
}
The main function that performs the alignment is static T* AlignedAddr(char* addr)
. Basically for an N-element type T
array we allocate enough size for (N+1) elements and return the lowest aligned address inside the allocated area.
To enable other methods in std::vector<T>
one would need to implement them using data_
and size_
individually. This worked for me since I usually only use few of them.