What is the recommended way to align memory in C++11

前端 未结 4 589
难免孤独
难免孤独 2020-12-02 05:50

I am working on a single producer single consumer ring buffer implementation.I have two requirements:

  1. Align a single heap allocated instance of a ring buffer to
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 06:10

    I don't know if it is the best way to align memory allocated with a new operator, but it is certainly very simple !

    This is the way it is done in thread sanitizer pass in GCC 6.1.0

    #define ALIGNED(x) __attribute__((aligned(x)))
    
    static char myarray[sizeof(myClass)] ALIGNED(64) ;
    var = new(myarray) myClass;
    

    Well, in sanitizer_common/sanitizer_internal_defs.h, it is also written

    // Please only use the ALIGNED macro before the type.
    // Using ALIGNED after the variable declaration is not portable!        
    

    So I do not know why the ALIGNED here is used after the variable declaration. But it is an other story.

提交回复
热议问题