malloc 和 HeapAlloc 之间有根本区别吗(除了可移植性之外)?
由于各种原因,我正在尝试将代码从 C 运行时移植到使用 Windows 堆 API 的代码。我遇到了一个问题:如果我将 malloc
/calloc
/realloc
/free
调用重定向到 HeapAlloc
/HeapReAlloc
/HeapFree
(使用GetProcessHeap
作为句柄),内存似乎分配正确(没有坏指针返回,并且没有抛出异常),但是我正在移植的库由于某种原因说“无法分配内存”。
我已经用 Microsoft CRT(它使用下面的堆 API)和另一家公司的运行时库(它使用下面的全局内存 API)进行了尝试;这两者的 malloc 都可以很好地与库配合使用,但由于某种原因,直接使用堆 API 不起作用。
我检查过分配量并不太大(>= 0x7FFF8 字节),事实并非如此。
我能想到的唯一问题是内存对齐;是这样吗?或者除此之外,堆 API 和 CRT 内存 API 之间是否存在我不知道的根本区别?
如果是这样,那是什么?如果不是,那么为什么静态 Microsoft CRT(包含在 Visual Studio 中)在调用 之前在
?我怀疑有什么不同,但我想不出它可能是什么。malloc
/calloc
中采取一些额外的步骤>堆分配
谢谢你!
I'm having code that, for various reasons, I'm trying to port from the C runtime to one that uses the Windows Heap API. I've encountered a problem: If I redirect the malloc
/calloc
/realloc
/free
calls to HeapAlloc
/HeapReAlloc
/HeapFree
(with GetProcessHeap
for the handle), the memory seems to be allocated correctly (no bad pointer returned, and no exceptions thrown), but the library I'm porting says "failed to allocate memory" for some reason.
I've tried this both with the Microsoft CRT (which uses the Heap API underneath) and with another company's run-time library (which uses the Global Memory API underneath); the malloc for both of those works well with the library, but for some reason, using the Heap API directly doesn't work.
I've checked that the allocations aren't too big (>= 0x7FFF8 bytes), and they're not.
The only problem I can think of is memory alignment; is that the case? Or other than that, is there a fundamental difference between the Heap API and the CRT memory API that I'm not aware of?
If so, what is it? And if not, then why does the static Microsoft CRT (included with Visual Studio) take some extra steps in malloc
/calloc
before calling HeapAlloc
? I'm suspecting there's a difference but I can't think of what it might be.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如我通过艰难的方式发现的那样...
区别并不是根本性的,但是
HeapReAlloc
(使用RtlReAllocateHeap
)确实不会自动处理空指针作为调用HeapAlloc的提示;它反而失败了。As I found out the hard way...
The difference isn't fundamental, but
HeapReAlloc
(which usesRtlReAllocateHeap
) does not automatically treat a null pointer as a hint to callHeapAlloc
; it fails instead.另一个重要的区别:
具有未定义的行为< /a>,而
定义良好(未执行任何操作)。
更新 2021:
HeapFree
文档 已于 2021 年 1 月更新,现在声明:老实说,目前尚不清楚这是否适用于特定 SDK 版本或以后。可以肯定的是,该参数已在 10.0.16299.0 上标有
_Frees_ptr_opt_
,因此可能只是一个文档问题。Another important difference:
has undefined behavior, while
is well defined (no operation performed).
UPDATE 2021:
The
HeapFree
documentation has been updated in January 2021, and now states:Honestly, it is not clear whether this applies since a specific SDK release or since ever. For sure that parameter was marked with
_Frees_ptr_opt_
already on 10.0.16299.0, so probably was just a documentation issue.