Using a vector for this is probably wrong (bad practice) - even though vector might only allocate once and even if you wrote the custom allocator - since you want the buffer to be static and shouldn't be moved. Additionally the vector is supposed to own the memory and clean up(release) after itself (where as you state that the memory for the elements have been pre-allocated), so vector does not seem like a good fit for a container to begin with.
Cast to aligned pointer type and use a span
c++20
When/if you have have c++20 you could use a span
as your "array view" basicly.
Until that, just use an aligned type and cast to that.
#include <iostream>
#include <type_traits>
#include <utility>
#include <span>
struct alignas(0x1000) Mine {
unsigned char a[64];
};
typename std::aligned_storage<sizeof(unsigned char[64]), 0x1000>::type mem_seg[10]; //<-- or your own allocated storage
int main() {
std::cout << "size: " << sizeof(Mine) << std::endl;
std::cout << "alignment: " << alignof(Mine) << std::endl;
Mine* begin_ = reinterpret_cast<Mine*>(&mem_seg);
Mine* end_ = reinterpret_cast<Mine*>(&mem_seg) + 10;
//pre-c++20 you done, work with the pointers 'normally'
//c++20 gives you span: (an array view of sorts)
//that works a lot like a normal container (except it's fixed size)
std::span<Mine> mine_view(begin_, end_);
for(auto& e : mine_view) {
std::cout << "address check: " << &e << std::endl;
}
return 0;
}
https://godbolt.org/z/H3BDof
The above prints the following (example):
size: 4096
alignment: 4096
address check: 0x604000
address check: 0x605000
address check: 0x606000
address check: 0x607000
address check: 0x608000
address check: 0x609000
address check: 0x60a000
address check: 0x60b000
address check: 0x60c000
address check: 0x60d000