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

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

        在 DLL 中分配内存并将指向它的指针指向客户端应

        时间:2023-10-07
          <bdo id='dvcpK'></bdo><ul id='dvcpK'></ul>
            <tbody id='dvcpK'></tbody>
          <i id='dvcpK'><tr id='dvcpK'><dt id='dvcpK'><q id='dvcpK'><span id='dvcpK'><b id='dvcpK'><form id='dvcpK'><ins id='dvcpK'></ins><ul id='dvcpK'></ul><sub id='dvcpK'></sub></form><legend id='dvcpK'></legend><bdo id='dvcpK'><pre id='dvcpK'><center id='dvcpK'></center></pre></bdo></b><th id='dvcpK'></th></span></q></dt></tr></i><div id='dvcpK'><tfoot id='dvcpK'></tfoot><dl id='dvcpK'><fieldset id='dvcpK'></fieldset></dl></div>
          <legend id='dvcpK'><style id='dvcpK'><dir id='dvcpK'><q id='dvcpK'></q></dir></style></legend>

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

                <tfoot id='dvcpK'></tfoot>

                  本文介绍了在 DLL 中分配内存并将指向它的指针指向客户端应用程序是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用动态加载 DLL 的 exe.DLL 中的函数在堆上分配内存并将指向该内存的指针传递给 exe.

                  I'm using an exe which dynamically loads a DLL. A function in the DLL allocates memory on the heap and passes a pointer to that memory to the exe.

                  一位前辈说这样做是不好的做法.他说,如果我不得不在 exe 和 DLL 之间共享内存,则 exe 必须分配内存并将指向该内存的指针传递给 DLL,反之亦然.这是真的?为什么?

                  A senior says that it is bad practice to do so. He says that if I ever have to share memory between the exe and the DLL, the exe has to allocate memory and pass a pointer to that to the DLL, and not vice versa. Is this true? Why?

                  就我而言,我计划在 DLL 本身内部分配和释放内存.

                  In my case, I planned to allocate and deallocate memory inside the DLL itself.

                  推荐答案

                  以下是让调用者提供指针的一些原因:

                  Here are some reasons for having the caller supply a pointer:

                  1. 对称所有权语义.其他几个答案已经解释了这一点.
                  2. 避免分配器和解除分配器不匹配.如美学家的回答中所述,如果 DLL分配一个指针并返回它,调用者必须调用相应的释放器来释放它.这不一定是微不足道的:DLL 可能静态链接到一个版本,例如 malloc/free.exe 链接到malloc/free 的不同版本.(例如,DLL 可能使用发布版本,而 .exe 使用专门的调试版本.)
                  3. 灵活性.如果 DLL 用于一般用途,则让调用方分配内存可为调用方提供更多选择.假设调用者不想使用 malloc 而是希望从某个特定的内存池分配内存.也许在这种情况下,调用者可以提供指向堆栈上分配的内存的指针.如果 DLL 自己分配内存,则调用者没有任何这些选项.
                  1. Symmetric ownership semantics. This is already explained by several other answers.
                  2. Avoids mismatching the allocator and deallocator. As mentioned in Aesthete's answer, if the DLL allocates a pointer and returns it, the caller must call the corresponding deallocator to free it. This is not necessarily trivial: the DLL might be statically linked against one version of, say, malloc/free while the .exe is linked against a different version of malloc/free. (For example, the DLL could be using release versions while the .exe is using specialized debug versions.)
                  3. Flexibility. If the DLL is meant for general use, having the caller allocate the memory gives the caller more options. Suppose the caller doesn't want to use malloc and instead wants memory to be allocated from some specific memory pool. Maybe it's a case where the caller could provide a pointer to memory allocated on the stack. If the DLL allocated the memory itself, the caller does not have any of these options.

                  (第二点和第三点也可以通过让 .exe 提供一个分配器/释放器供 DLL 代码使用来解决.)

                  (The second and third points also mostly can be addressed by having the .exe supply an allocator/deallocator for the DLL code to use.)

                  这篇关于在 DLL 中分配内存并将指向它的指针指向客户端应用程序是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:访问受限的朋友类 下一篇:干净的 C++ 粒度朋友等价物?(答案:律师-客户成语

                  相关文章

                  最新文章

                  • <bdo id='D5kUs'></bdo><ul id='D5kUs'></ul>

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

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