How to make tr1::array allocate aligned memory?

后端 未结 1 865
孤街浪徒
孤街浪徒 2021-01-06 07:41

You can allocate a std::vector which allocates aligned heap memory by defining your own allocator. You can allocate a c-style array on the stack using declspec align. But ca

相关标签:
1条回答
  • 2021-01-06 08:32

    tr1::array (and std::array and boost::array) are POD, so the memory occupied by the contents is coincident with the memory of the array. So, allocate the array however you need to, and construct it with placement new.

    typedef std::tr1::array< MyClass, ary_sz > AryT;
    void *array_storage = aligned_allocation( sizeof( AryT ) );
    AryT *ary = new( array_storage ) AryT( initial_value );
    
    0 讨论(0)
提交回复
热议问题