﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Panda-随笔分类-windows</title><link>http://www.cppblog.com/Panda/category/11684.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 08 Sep 2009 15:43:28 GMT</lastBuildDate><pubDate>Tue, 08 Sep 2009 15:43:28 GMT</pubDate><ttl>60</ttl><item><title>HeapAlloc GlobalAlloc new等内存分配的区别</title><link>http://www.cppblog.com/Panda/archive/2009/09/06/95435.html</link><dc:creator>ChinaPanda</dc:creator><author>ChinaPanda</author><pubDate>Sun, 06 Sep 2009 13:50:00 GMT</pubDate><guid>http://www.cppblog.com/Panda/archive/2009/09/06/95435.html</guid><wfw:comment>http://www.cppblog.com/Panda/comments/95435.html</wfw:comment><comments>http://www.cppblog.com/Panda/archive/2009/09/06/95435.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Panda/comments/commentRss/95435.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Panda/services/trackbacks/95435.html</trackback:ping><description><![CDATA[<div class="articleContent" id="articleBody">
<p>
GlobalAlloc是标准内存管理函数，标准内存管理函数都是操作进程的默认堆，所以这个函数是从进程的默认堆中分配内存空间，分配的空间可以是可移动的也可以是不可移动的。可移动的内存是指Windows在需要的时候可以将这个内存移动到另外一个地址.</p>
<p>&nbsp;<wbr></p>
<p>关于GlobalAlloc and&nbsp;<wbr>LocalAlloc,from
MSDN&nbsp;<wbr>&nbsp;<wbr><br>
&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><br>
&nbsp;<wbr> The global and local functions supported for
porting from 16-bit code, or maintaining
source&nbsp;<wbr>code compatibility with 16-bit Windows. The
global and local functions are slower &nbsp;<wbr> than other
memory management functions and do not provide as many features.
Therefore,&nbsp;<wbr> new &nbsp;<wbr> applications
should use the heap functions.However, the global functions are
still used with DDE and the clipboard functions.
&nbsp;<wbr><br>
&nbsp;<wbr> &nbsp;<wbr><br>
&nbsp;<wbr> Windows memory management does not provide a
separate local heap and global heap, as 16-bit Windows does. As a
result, <font color="#ff0000"><strong>there is no difference
between the memory objects allocated by the GlobalAlloc and
LocalAlloc functions</strong></font>. In addition, the change from
a 16-bit segmented memory model to a 32-bit virtual memory model
has made some of the related global and local functions and their
options unnecessary or meaningless. For example, <font color="#ff0000"><strong>there are no longer near and far
pointers,</strong></font> because both local and global allocations
return 32-bit virtual addresses.<br>
&nbsp;<wbr><br>
&nbsp;<wbr> Memory objects allocated by GlobalAlloc and
LocalAlloc are in private, committed pages with read/write access
that cannot be accessed by other processes. Memory allocated by
using GlobalAlloc with GMEM_DDESHARE is not actually shared
globally as it is in 16-bit Windows. This value has no effect and
is available only for compatibility. <font color="#ff0000">Applications requiring shared memory for other purposes
must use file-mapping objects</font>. Multiple processes can map a
view of the same file-mapping object to provide named shared
memory. For more information, see File Mapping.<br>
<br>
HeapAllock是堆管理函数，堆管理函数可以操作非默认堆（当然也可以操作默认堆），创建一个堆是用HeapCreate，这个函数返回一个堆句
柄，然后可以用在HeapAllock函数中，即从返回的这个堆中申请内存空间，HeapAllock申请的内存只能是不可以移动的.<br>
<br>
而new则是c++的标准函数，在Windows的VC++编译器中，new在申请内存时最终调用的是GlabalAlloc，不过new还可以调用类的构造函数.<br>
<br>
Windows的内存管理除了标准内存管理函数和堆管理函数之外，还有更加底层的虚拟内存管理函数，VirtualAlloc就是一个虚拟内存管理函数.</p>
<p>&nbsp;<wbr></p>
<p>Personal Comprehension</p>
<p>
GlobalAlloc分配的内存,还可以调用GlobalLock锁定该内存块(该函数可以被多次调用),在我们没有调用GlobalUnlock之
前,该内存块会一直保持有效(即使调用了GlobalFree函数,但如果该内存的锁计数不为0,该内存块也不会被释放掉,依然保持有效)!而如果只调用
一次delete,则使用new所分配的内存就会被释放掉.</p>
</div><img src ="http://www.cppblog.com/Panda/aggbug/95435.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Panda/" target="_blank">ChinaPanda</a> 2009-09-06 21:50 <a href="http://www.cppblog.com/Panda/archive/2009/09/06/95435.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>