除了可移植性之外,malloc和HeapAlloc之间是否有根本区别? [英] Is there a fundamental difference between malloc and HeapAlloc (aside from the portability)?

查看:178
本文介绍了除了可移植性之外,malloc和HeapAlloc之间是否有根本区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于各种原因,我正在尝试将代码从C运行时移植到使用Windows Heap API的代码.我遇到了一个问题:如果我将malloc/calloc/realloc/free调用重定向到HeapAlloc/HeapReAlloc/HeapFree(句柄为GetProcessHeap),内存似乎分配正确(没有返回错误的指针,也没有引发异常),但是由于某些原因,我要移植的库显示分配内存失败".

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.

我已经使用Microsoft CRT(在下面使用Heap API)和另一家公司的运行时库(在下面使用Global Memory API)进行了尝试.两者的malloc都可以很好地与库一起使用,但是由于某些原因,直接使用Heap API无效.

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.

我已经检查过分配的大小不是很大(> = 0x7FFF8字节),并且不是.

I've checked that the allocations aren't too big (>= 0x7FFF8 bytes), and they're not.

我唯一想到的问题是内存对齐;是这样吗?还是除此之外,我不知道的Heap API和CRT memory API之间有根本区别吗?

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?

如果是,那是什么?如果不是,那么为什么 static Microsoft CRT(Visual Studio附带)为什么在调用HeapAlloc之前在malloc/calloc中采取了一些额外的步骤?我怀疑有区别,但是我想不出是什么.

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.

谢谢!

推荐答案

当我发现困难的方法时...

As I found out the hard way...

区别不是根本的,但是HeapReAlloc(使用RtlReAllocateHeap)不会 not 自动将空指针作为调用HeapAlloc的提示.相反,它失败了.

The difference isn't fundamental, but HeapReAlloc (which uses RtlReAllocateHeap) does not automatically treat a null pointer as a hint to call HeapAlloc; it fails instead.

这篇关于除了可移植性之外,malloc和HeapAlloc之间是否有根本区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆