在Windows中,如何用“new”替换“GlobalAlloc”?
在 Windows 中,如何用 new
替换 GlobalAlloc
?
你好, 我有这段代码(来自这里: “从邮槽读取”) 使用 GlobalAlloc 分配内存。
DWORD cbRead = 0;
LPTSTR lpszBuffer = (LPTSTR) ::GlobalAlloc(GPTR, cbMessage); //cbMessage is from a call to GetMailslotInfo
if( NULL == lpszBuffer )
return FALSE;
lpszBuffer[0] = '\0';
BOOL fResult = ::ReadFile(hSlot, lpszBuffer, cbMessage, &cbRead, 0);
if (fResult)
{
_tprintf(TEXT("Contents of the mailslot: %s\n"), lpszBuffer);
}
::GlobalFree((HGLOBAL) lpszBuffer);
我想更改代码并使用智能指针而不是裸露的 LPTSTR
(并摆脱 GlobalFree
)和 new
而不是全局分配
。 cbMessage
是“下一条消息的大小,以字节为单位”,所以我需要像 malloc
这样适用于无类型内存的东西,是否有任何形式的 new< /code> 适合我的情况吗?
In Windows, how can I replace GlobalAlloc
with new
?
Hello,
I have this snippet of code (from here: "Reading from a Mailslot")
that allocate memory with GlobalAlloc
.
DWORD cbRead = 0;
LPTSTR lpszBuffer = (LPTSTR) ::GlobalAlloc(GPTR, cbMessage); //cbMessage is from a call to GetMailslotInfo
if( NULL == lpszBuffer )
return FALSE;
lpszBuffer[0] = '\0';
BOOL fResult = ::ReadFile(hSlot, lpszBuffer, cbMessage, &cbRead, 0);
if (fResult)
{
_tprintf(TEXT("Contents of the mailslot: %s\n"), lpszBuffer);
}
::GlobalFree((HGLOBAL) lpszBuffer);
I would like to change the code and use a smart pointer instead of a bare LPTSTR
(and to get rid of GlobalFree
) and new
instead of GlobalAlloc
.cbMessage
is "The size of the next message, in bytes" and so I need something like malloc
which works for untyped memory, is there any form of new
suitable to my case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,你不能。
不同的内存分配函数(
GlobalAlloc
、malloc
、new
、SysAlloc
、VirtualAlloc
、HeapAlloc)的存在是因为它们以不同的方式分配内存、不同的位置、不同的大小、不同的标记、使用不同的底层管理器以及无数其他差异。其中一些位于本地堆中,一些是全局的,一些是虚拟的,一些没有指定,其他的分配内存并用它做事,例如SysAllocString,其他的与COM一起工作,如CoGetMalloc,等等。如果调用指定使用一个分配器,则可能有底层代码将内存传递给另一个进程或需要该分配器的某些其他行为。您可以尝试使用其他的,但这可能是未定义的行为。
为了将智能指针与其他分配器一起使用,您可以执行以下操作。最简单的是将它们作为分配器和释放器函数提供给智能指针类,使其能够正确处理事情。根据您选择的指针,这可能需要一些调整,或者您可能必须组合一个能够与该分配器一起使用的基本智能指针。
对于常见的分配器,MFC 和/或 ATL 通常具有与一个或多个专用分配器一起使用的智能指针和辅助函数。如果可以使用这些,您可以研究一下。
Generally, you can't.
The different memory allocation functions (
GlobalAlloc
,malloc
,new
,SysAlloc
,VirtualAlloc
,HeapAlloc
) exist because they allocate memory in different ways, different places, different sizes, tag it differently, use different underlying managers, and myriad other differences. Some of them are in the local heap, some global, some virtual, some don't specify, others that allocate memory and do things with it, such as SysAllocString, others that work with COM like CoGetMalloc, and so on.If a call specifies that one allocator be used, there's likely underlying code that passes the memory to another process or some other behavior requiring that allocator. You can try to use a different one, but it's likely to be undefined behavior.
In order to use smart pointers with the other allocators, there are a few things you can do. The simplest is providing them as the allocator and deallocator functions to the smart pointer class, allowing it to handle things properly. Depending on your pointer of choice, this may require some tweaking, or you may have to put together a basic smart pointer capable of working with that allocator.
For common ones, MFC and/or ATL often have smart pointers and helper functions that work with one or more of the specialized allocators. If using those is possible, you may look into that.
这使用智能指针来管理内存,因此您不需要显式释放它。 (增强scoped_array。)
This uses a smart pointer to manage the memory, so you don't need to explicitly free it. (A boost scoped_array.)