<small id='SDvS8'></small><noframes id='SDvS8'>

        <legend id='SDvS8'><style id='SDvS8'><dir id='SDvS8'><q id='SDvS8'></q></dir></style></legend>
      1. <i id='SDvS8'><tr id='SDvS8'><dt id='SDvS8'><q id='SDvS8'><span id='SDvS8'><b id='SDvS8'><form id='SDvS8'><ins id='SDvS8'></ins><ul id='SDvS8'></ul><sub id='SDvS8'></sub></form><legend id='SDvS8'></legend><bdo id='SDvS8'><pre id='SDvS8'><center id='SDvS8'></center></pre></bdo></b><th id='SDvS8'></th></span></q></dt></tr></i><div id='SDvS8'><tfoot id='SDvS8'></tfoot><dl id='SDvS8'><fieldset id='SDvS8'></fieldset></dl></div>
          <bdo id='SDvS8'></bdo><ul id='SDvS8'></ul>
      2. <tfoot id='SDvS8'></tfoot>

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

        What can I use instead of std::aligned_alloc in MS Visual Studio 2013?(在 MS Visual Studio 2013 中,我可以使用什么来代替 std::aligned_alloc?)

              <tbody id='Ah6II'></tbody>
              <bdo id='Ah6II'></bdo><ul id='Ah6II'></ul>

              • <tfoot id='Ah6II'></tfoot>
                1. <legend id='Ah6II'><style id='Ah6II'><dir id='Ah6II'><q id='Ah6II'></q></dir></style></legend>

                  <small id='Ah6II'></small><noframes id='Ah6II'>

                  <i id='Ah6II'><tr id='Ah6II'><dt id='Ah6II'><q id='Ah6II'><span id='Ah6II'><b id='Ah6II'><form id='Ah6II'><ins id='Ah6II'></ins><ul id='Ah6II'></ul><sub id='Ah6II'></sub></form><legend id='Ah6II'></legend><bdo id='Ah6II'><pre id='Ah6II'><center id='Ah6II'></center></pre></bdo></b><th id='Ah6II'></th></span></q></dt></tr></i><div id='Ah6II'><tfoot id='Ah6II'></tfoot><dl id='Ah6II'><fieldset id='Ah6II'></fieldset></dl></div>
                  本文介绍了在 MS Visual Studio 2013 中,我可以使用什么来代替 std::aligned_alloc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  I would like to use C++11's std::aligned_alloc, but unfortunately it isn't available with Microsoft Visual Studio 2013.

                  我正在考虑,intsead,自己实现 aligned_alloc.一个实现应该是什么样的?以下示例无法编译,因为它无法从 void* 转换为 void*&.

                  I'm considering, intsead, implementing aligned_alloc on my own. How should an implementation look like? The following for example doesn't compile, because it cannot convert from void* to 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;
                   }
                  

                  推荐答案

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

                  Disclaimer: I didn't thoroughly test this code.

                  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));
                  }
                  

                  说明:

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

                  The alignment is adjusted to alignof(void*) if it's less than that, because, as we will see, we need to store a (properly aligned) void*.

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

                  We need size + alignment - 1 bytes to ensure that we can find a size byte block in there with the right alignment, plus an additional sizeof(void*) bytes to store the pointer returned by ::operator new so that we can free it later.

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

                  We allocate this memory with ::operator new and store the returned pointer in allocated_mem. We then add sizeof(void*) bytes to allocated_mem and store the result in aligned_mem. At this point, we haven't aligned it yet.

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

                  At point #1, the memory block and the two points look like this:

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

                  std::align 调用调整 aligned_mem 以获得所需的对齐方式.在第 2 点,它现在看起来像这样:

                  The std::align call adjusts aligned_mem to obtain the desired alignment. At point #2, it now looks like this:

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

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

                  Because we started at sizeof(void*) bytes past allocated_mem, the "extra space" is at least sizeof(void*) bytes. Moreover, aligned_mem is correctly aligned for void*, so we can store a void* right before it. At point #3, the block of memory looks like this

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

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

                  As to aligned_free, it simply reads the pointer stored there and passes it to ::operator delete.

                  这篇关于在 MS Visual Studio 2013 中,我可以使用什么来代替 std::aligned_alloc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                2. <tfoot id='brmkS'></tfoot>
                    • <bdo id='brmkS'></bdo><ul id='brmkS'></ul>

                          <small id='brmkS'></small><noframes id='brmkS'>

                        • <legend id='brmkS'><style id='brmkS'><dir id='brmkS'><q id='brmkS'></q></dir></style></legend>
                            <i id='brmkS'><tr id='brmkS'><dt id='brmkS'><q id='brmkS'><span id='brmkS'><b id='brmkS'><form id='brmkS'><ins id='brmkS'></ins><ul id='brmkS'></ul><sub id='brmkS'></sub></form><legend id='brmkS'></legend><bdo id='brmkS'><pre id='brmkS'><center id='brmkS'></center></pre></bdo></b><th id='brmkS'></th></span></q></dt></tr></i><div id='brmkS'><tfoot id='brmkS'></tfoot><dl id='brmkS'><fieldset id='brmkS'></fieldset></dl></div>
                              <tbody id='brmkS'></tbody>