I'm working on a bit of a thought experiment here -- I'm trying to actually make my life easier here. I'm working with a data structure that, among other things, contains a couple of arrays of elements that are kept in sorted order. I allocate these data structures in fixed-size blocks to make for easier memory placement, and also to (at a future time) make for easier reading/writing from stable storage. Here is the code I'm working with so far:
#include <limits> const int NODE_SIZE = 512; template <typename K, typename D> class Node { long next; short num; K* keys; D* data; public: Node( int l, int order ); }; // num is calculated by something like this... num = NODE_SIZE - sizeof( Node<K,D> ) - sizeof( long ); num /= (sizeof( D ) + sizeof( K )); // Constructor // Will be called with a placement-new and given a NODE_SIZE // byte block of memory, aligned at NODE_SIZE template<typename K, typename D> Node<K,D>::Node( int n ) : num ( n ), next( 0 ) { keys = reinterpret_cast<K*>(reinterpret_cast<char*>(&next) + sizeof( *this )); int numbytes = num*sizeof(K); // Make sure we're aligned to a void *. if ( numbytes % sizeof( void * ) ) { numbytes = (numbytes / sizeof( void * )+1)*sizeof( void * ); } // Align to the number of bytes in a void * data = reinterpret_cast<D*>( reinterpret_cast<char*>(keys)+numbytes); for( int i=0; i<num; i++ ) keys[i] = std::numeric_limits<K>::max(); } Since the elements in key are in sorted order, I would really like to be able to use a std::vector and std::vector so I can use somebody else's vector insertion code instead of writing my own (not that it's hard, but why reinvent the wheel?).
Also, is there a cleaner way of setting up my pointers for keys and data? Any assistance or suggestions would be welcome.
std::vectorwith an allocator. I'm not certain, because I neither understand exactly what you are doing, nor do I fully understand allocators, but my ignorance matches where I think you and allocators overlap. :)newto put astd::vectorat a specific address, but that would only put the vector's member variables (typically three pointers and a possibly-empty allocator) into that memory, the vector's elements would be stored separately in memory obtained from its allocator. As the comment above says, you could use a custom allocator that is initialized with a pointer to the storage you've already allocated, and which returns pointers to that memory when requested to allocate memory, but doing so is surprisingly complicated and difficult to get right, unfortunately.