在MS Visual Studio 2013中,我可以使用什么代替std :: aligned_alloc?

use*_*183 3 c++ memory-alignment dynamic-memory-allocation visual-studio-2013

我想使用C++ 11 std::aligned_alloc,但遗憾的是它不适用于Microsoft Visual Studio 2013.

我正在考虑,intsead,aligned_alloc我自己实施.实现应该如何?以下例如不编译,因为它无法转换void*void*&.

 template<typename T>
 T* aligned_alloc( std::size_t size, std::size_t align )
 {
        T* ptr = new T[size + align];
        std::align(align, size, reinterpret_cast<void*>(ptr), align + size);
        return ptr;
 }
Run Code Online (Sandbox Code Playgroud)

T.C*_*.C. 8

免责声明:我没有彻底测试这段代码.

void* aligned_alloc(std::size_t size, std::size_t alignment){
    if(alignment < alignof(void*)) { 
        alignment = alignof(void*); 
    }
    std::size_t space = size + alignment - 1;
    void* allocated_mem = ::operator new(space + sizeof(void*));
    void* aligned_mem = static_cast<void*>(static_cast<char*>(allocated_mem) + sizeof(void*)); 
    ////////////// #1 ///////////////
    std::align(alignment, size, aligned_mem, space);
    ////////////// #2 ///////////////
    *(static_cast<void**>(aligned_mem) - 1) = allocated_mem;
    ////////////// #3 ///////////////
    return aligned_mem;
}

void aligned_free(void* p) noexcept {
    ::operator delete(*(static_cast<void**>(p) - 1));
}
Run Code Online (Sandbox Code Playgroud)

说明:

alignof(void*)如果它小于那个,则调整对齐,因为正如我们将看到的,我们需要存储(正确对齐)void*.

我们需要size + alignment - 1字节来确保我们可以size在那里找到具有正确对齐的字节块,以及sizeof(void*)用于存储返回的指针的附加字节,::operator new以便我们以后可以释放它.

我们用这个内存分配::operator new并存储返回的指针allocated_mem.然后我们将sizeof(void*)字节添加到allocated_mem并存储结果aligned_mem.在这一点上,我们还没有对齐它.

在#1点,内存块和两个点看起来像这样:

              aligned_mem (not actually aligned yet)
              V
+-------------+-----------------------------------------+
|sizeof(void*)|    size + alignment - 1  bytes          |
+-------------+-----------------------------------------+
^
allocated_mem points here
Run Code Online (Sandbox Code Playgroud)

std::align呼叫调整aligned_mem以获得所需的取向.在#2点,它现在看起来像这样:

                      aligned_mem (correctly aligned now)
                      V
+---------------------+---------------------------------+
| extra space         |  at least size bytes            |
+---------------------+---------------------------------+
^
allocated_mem points here
Run Code Online (Sandbox Code Playgroud)

因为我们从sizeof(void*)字节过去开始allocated_mem,所以"额外空间"至少是sizeof(void*)字节.而且,aligned_mem正确对齐void*,所以我们可以void*在它之前存储一个权利.在#3点,内存块看起来像这样

                      aligned_mem (returned to caller)
                      V
+---------------+-----+---------------------------------+
|               |  ^  |  at least size bytes            |
+---------------+--+--+---------------------------------+
^                  |
allocated_mem      value of allocated_mem
points here        stored here
Run Code Online (Sandbox Code Playgroud)

至于aligned_free,它只是读取存储在那里的指针并将其传递给::operator delete.


Rob*_*son 5

在 Windows 中,它是_aligned_malloc_aligned_free,可以在 malloc.h 中找到。std 实现(alignof/alignas)在 VS 2015 中。它在 2013 中不可用。

  • 显然根据我的测试,aligned_alloc或aligned_malloc在vs 2017中仍然没有实现。所以即使在vs2017中你也必须使用上面提到的_aligned_malloc。 (3认同)