LocalAlloc Vs GlobalAlloc Vs malloc Vs新 [英] LocalAlloc Vs GlobalAlloc Vs malloc Vs new

查看:88
本文介绍了LocalAlloc Vs GlobalAlloc Vs malloc Vs新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在各种链接上进行了搜索,但仍然存在疑问。



我不理解 LocalAlloc vs GlobalAlloc vs malloc vs new 内存分配。



我通过MSDN的此链接:



比较内存分配方法



请解释以下语句:


malloc 函数具有依赖于运行时的缺点。 new 运算符的缺点在于依赖于编译器和语言



解决方案

摘录自雷蒙德·陈的OldNewThing


在16位Windows的日子里,差异是巨大的。



在16位位Windows,通过称为
选择器的值访问内存,每个值最多可寻址64K。有一个
默认选择器,称为数据选择器。相对于数据选择器对所谓的
近指针执行了操作。以
为例,如果您有一个近指针p,其值为0x1234,而
数据选择器为0x012F,那么当您编写* p时,您正在访问
的内存位于012F:1234。 (当您声明一个指针时,默认情况下它接近
。如果要使用一个远指针,则必须明确地说出FAR。)



重要提示:Near指针总是相对于选择器,通常是
数据选择器。



GlobalAlloc函数分配了一个选择器,该选择器可用于
访问您要求的内存。您可以使用远指针访问该选择器中的内存
。 远指针是选择器
与近指针组合。 (请记住,相对于选择器,近指针是
;当将近指针与
合适的选择器结合使用时,您会得到远指针。)



程序和DLL的每个实例都有其自己的数据选择器,称为
称为HINSTANCE。因此,如果您有近指针p并通过* p从程序可执行文件访问
,则它访问的内存相对于程序实例的HINSTANCE的
。如果从DLL访问它,则
将获得与DLL的HINSTANCE相关的内存。



因此,在16位Windows中,LocalAlloc和GlobalAlloc
函数完全不同! LocalAlloc返回了接近
的指针,而GlobalAlloc返回了选择器。



您要在模块之间传递的指针必须采用
的形式远指针,因为每个模块都有一个不同的默认
选择器。如果要将内存所有权转移到另一个
模块,则必须使用GlobalAlloc,因为这允许接收者
调用GlobalFree释放它。



<即使在Win32中,也必须注意不要混淆全局堆中的本地堆
。从一个分配的内存不能在
个上释放。随着
向Win32的过渡,关于近和远指针的所有怪异都消失了。但是本地堆函数和全局
堆函数仍然是两个不同的堆接口。


此外,链接明确指出,


从32位Windows开始,GlobalAlloc和LocalAlloc被实现为
作为包装函数,该包装函数使用$的句柄调用HeapAlloc b $ b进程的默认堆,如果无法分配内存,可以指示HeapAlloc引发
异常,这是LocalAlloc无法使用的
的功能。




对于您对 malloc与new 的困惑,Billy ONeal的回答很清楚地总结了这一点。



对于 malloc和HeapAlloc 之间的区别,
David Heffernan's和路易斯·米格尔·瓦帕亚(Luis Miguel Huapaya)的答案相结合,给出了完美的解决方案:




  • malloc 是可移植的,是标准的一部分。 malloc (和其他C运行时堆函数)是模块相关的,这意味着如果您从一个代码中调用 malloc 模块(即DLL),则应在同一模块的代码内调用 free ,否则可能会遭受相当严重的堆损坏。

  • HeapAlloc 不可移植,它是Windows API函数。使用 HeapAlloc GetProcessHeap 代替 malloc ,包括重载 new delete 运算符可以利用此类,使您可以在模块之间传递动态分配的对象,而不必担心如果在指向一个内存块的指针传递到外部模块后,在一个模块的代码中分配了内存,并在另一个模块的代码中释放了内存,则会导致内存损坏。


I have searched for this on various links, but still the doubt persist.

I do not understand the difference between LocalAlloc vs GlobalAlloc vs malloc vs new for memory allocation.

I have gone through this link of MSDN:

Comparing Memory Allocation Methods

Please explain the following statement:

The malloc function has the disadvantage of being run-time dependent. The new operator has the disadvantage of being compiler dependent and language dependent

解决方案

Excerpts from Raymond Chen's OldNewThing

Back in the days of 16-bit Windows, the difference was significant.

In 16-bit Windows, memory was accessed through values called "selectors", each of which could address up to 64K. There was a default selector called the "data selector"; operations on so-called "near pointers" were performed relative to the data selector. For example, if you had a near pointer p whose value was 0x1234 and your data selector was 0x012F, then when you wrote *p, you were accessing the memory at 012F:1234. (When you declared a pointer, it was near by default. You had to say FAR explicitly if you wanted a far pointer.)

Important: Near pointers are always relative to a selector, usually the data selector.

The GlobalAlloc function allocated a selector that could be used to access the amount of memory you requested. You could access the memory in that selector with a "far pointer". A "far pointer" is a selector combined with a near pointer. (Remember that a near pointer is relative to a selector; when you combine the near pointer with an appropriate selector, you get a far pointer.)

Every instance of a program and DLL got its own data selector, known as the HINSTANCE. Therefore, if you had a near pointer p and accessed it via *p from a program executable, it accessed memory relative to the program instance’s HINSTANCE. If you accessed it from a DLL, you got memory relative to your DLL’s HINSTANCE.

Therefore, that in 16-bit Windows, the LocalAlloc and GlobalAlloc functions were completely different! LocalAlloc returned a near pointer, whereas GlobalAlloc returned a selector.

Pointers that you intended to pass between modules had to be in the form of "far pointers" because each module has a different default selector. If you wanted to transfer ownership of memory to another module, you had to use GlobalAlloc since that permitted the recipient to call GlobalFree to free it.

Even in Win32, you have to be careful not to confuse the local heap from the global heap. Memory allocated from one cannot be freed on the other. All the weirdness about near and far pointers disappeared with the transition to Win32. But the local heap functions and the global heap functions are nevertheless two distinct heap interfaces.

Also, the link specified by you clearly says that,

Starting with 32-bit Windows, GlobalAlloc and LocalAlloc are implemented as wrapper functions that call HeapAlloc using a handle to the process's default heap, and HeapAlloc can be instructed to raise an exception if memory could not be allocated, a capability not available with LocalAlloc.

For your confusion on malloc vs new, Billy ONeal's answer summarizes that pretty clearly.

For the difference between malloc and HeapAlloc, David Heffernan's and Luis Miguel Huapaya's answer combined gives the perfect solution::

  • malloc is portable, part of the standard. malloc (and other C runtime heap functions) are module dependant, which means that if you call malloc in code from one module (i.e. a DLL), then you should call free within code of the same module or you could suffer some pretty bad heap corruption.
  • HeapAlloc is not portable, it's a Windows API function. Using HeapAlloc with GetProcessHeap instead of malloc, including overloading new and delete operators to make use of such, allow you to pass dynamically allocated objects between modules and not have to worry about memory corruption if memory is allocated in code of one module and freed in code of another module once the pointer to a block of memory has been passed across to an external module.

这篇关于LocalAlloc Vs GlobalAlloc Vs malloc Vs新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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