在DLL中分配内存并将其指向客户端应用程序是不好的做法吗? [英] Is it bad practice to allocate memory in a DLL and give a pointer to it to a client app?
本文介绍了在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 some reasons for having the caller supply a pointer:
- 对称的所有权语义。 strong>这已经通过几个其他答案解释。
- 避免分配器和释放器不匹配。