Первый — использование массивов:
Код: Выделить всё
struct Foo {
char data[3]; // size is 3, my arch is 64-bit (8 bytes)
};
Foo array[4]; // total memory is 3 * 4 = 12 bytes.
// will this be padded to 16?
void testArray() {
Foo foo1 = array[0];
Foo foo2 = array[1]; // is foo2 pointing to a non-aligned location?
// should one expect issues here?
}
Код: Выделить всё
struct Pool {
Pool(std::size_t size = 256) : data(size), used(0), freed(0) { }
template
T * allocate() {
T * result = reinterpret_cast(&data[used]);
used += sizeof(T);
return result;
}
template
void deallocate(T * ptr) {
freed += sizeof(T);
if (freed == used) {
used = freed = 0;
}
}
std::vector data;
std::size_t used;
std::size_t freed;
};
void testPool() {
Pool pool;
Foo * foo1 = pool.allocate(); // points to data[0]
Foo * foo2 = pool.allocate(); // points to data[3],
// alignment issue here?
pool.deallocate(foo2);
pool.deallocate(foo1);
}
- Есть ли какие-либо проблемы с выравниванием в двух кодах? образцы?
- Если да, то как их можно исправить?
- Где я могу узнать об этом больше?
Я использую 64-битный процессор Intel i7 с Darwin GCC.
Но я также использую Linux, Windows (VC2008) для 32-битных и 64-битных систем.
Обновить 2
Пул теперь использует вектор вместо массива.
Подробнее здесь: https://stackoverflow.com/questions/647 ... -alignment