Does the alignas specifier work with 'new'?

后端 未结 2 1129
庸人自扰
庸人自扰 2020-12-16 16:42

My question is rather simple;

Does the alignas specifier work with \'new\'? That is, if a struct is defined to be aligned, will it be aligned when allocated with new

2条回答
  •  难免孤独
    2020-12-16 17:18

    No it does not. The struct will be padded to the alignment requested, but it will not be aligned. There is a chance, however, that this will be allowed in C++17 (the fact that this C++17 proposal exists should be pretty good proof this can't work in C++11).

    I have seen this appear to work with some memory allocators, but that was pure luck. For instance, some memory allocators will align their memory allocations to powers of 2 of the requested size (up to 4KB) as an optimization for the allocator (reduce memory fragmentation, possibly make it easier to reuse previously freed memory, etc...). However, the new/malloc implementations that are included in the OS X 10.7 and CentOS 6 systems that I tested do not do this and fail with the following code:

    #include 
    #include 
    
    struct alignas(8)   test_struct_8   { char data; };
    struct alignas(16)  test_struct_16  { char data; };
    struct alignas(32)  test_struct_32  { char data; };
    struct alignas(64)  test_struct_64  { char data; };
    struct alignas(128) test_struct_128 { char data; };
    struct alignas(256) test_struct_256 { char data; };
    struct alignas(512) test_struct_512 { char data; };
    
    int main() {
       test_struct_8   *heap_8   = new test_struct_8;
       test_struct_16  *heap_16  = new test_struct_16;
       test_struct_32  *heap_32  = new test_struct_32;
       test_struct_64  *heap_64  = new test_struct_64;
       test_struct_128 *heap_128 = new test_struct_128;
       test_struct_256 *heap_256 = new test_struct_256;
       test_struct_512 *heap_512 = new test_struct_512;
    
    #define IS_ALIGNED(addr,size)   ((((size_t)(addr)) % (size)) == 0)
    
       assert(IS_ALIGNED(heap_8, 8));
       assert(IS_ALIGNED(heap_16, 16));
       assert(IS_ALIGNED(heap_32, 32));
       assert(IS_ALIGNED(heap_64, 64));
       assert(IS_ALIGNED(heap_128, 128));
       assert(IS_ALIGNED(heap_256, 256));
       assert(IS_ALIGNED(heap_512, 512));
    
       delete heap_8;
       delete heap_16;
       delete heap_32;
       delete heap_64;
       delete heap_128;
       delete heap_256;
       delete heap_512;
    
    return 0;
    }
    

提交回复
热议问题