GlobalAlloc是標(biāo)準(zhǔn)內(nèi)存管理函數(shù),標(biāo)準(zhǔn)內(nèi)存管理函數(shù)都是操作進(jìn)程的默認(rèn)堆,所以這個(gè)函數(shù)是從進(jìn)程的默認(rèn)堆中分配內(nèi)存空間,分配的空間可以是可移動(dòng)的也可以是不可移動(dòng)的。可移動(dòng)的內(nèi)存是指Windows在需要的時(shí)候可以將這個(gè)內(nèi)存移動(dòng)到另外一個(gè)地址.
關(guān)于GlobalAlloc and LocalAlloc,from
MSDN
The global and local functions supported for
porting from 16-bit code, or maintaining
source code compatibility with 16-bit Windows. The
global and local functions are slower than other
memory management functions and do not provide as many features.
Therefore, new applications
should use the heap functions.However, the global functions are
still used with DDE and the clipboard functions.
Windows memory management does not provide a
separate local heap and global heap, as 16-bit Windows does. As a
result, there is no difference
between the memory objects allocated by the GlobalAlloc and
LocalAlloc functions. 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, there are no longer near and far
pointers, because both local and global allocations
return 32-bit virtual addresses.
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. Applications requiring shared memory for other purposes
must use file-mapping objects. Multiple processes can map a
view of the same file-mapping object to provide named shared
memory. For more information, see File Mapping.
HeapAllock是堆管理函數(shù),堆管理函數(shù)可以操作非默認(rèn)堆(當(dāng)然也可以操作默認(rèn)堆),創(chuàng)建一個(gè)堆是用HeapCreate,這個(gè)函數(shù)返回一個(gè)堆句
柄,然后可以用在HeapAllock函數(shù)中,即從返回的這個(gè)堆中申請內(nèi)存空間,HeapAllock申請的內(nèi)存只能是不可以移動(dòng)的.
而new則是c++的標(biāo)準(zhǔn)函數(shù),在Windows的VC++編譯器中,new在申請內(nèi)存時(shí)最終調(diào)用的是GlabalAlloc,不過new還可以調(diào)用類的構(gòu)造函數(shù).
Windows的內(nèi)存管理除了標(biāo)準(zhǔn)內(nèi)存管理函數(shù)和堆管理函數(shù)之外,還有更加底層的虛擬內(nèi)存管理函數(shù),VirtualAlloc就是一個(gè)虛擬內(nèi)存管理函數(shù).
Personal Comprehension
GlobalAlloc分配的內(nèi)存,還可以調(diào)用GlobalLock鎖定該內(nèi)存塊(該函數(shù)可以被多次調(diào)用),在我們沒有調(diào)用GlobalUnlock之
前,該內(nèi)存塊會(huì)一直保持有效(即使調(diào)用了GlobalFree函數(shù),但如果該內(nèi)存的鎖計(jì)數(shù)不為0,該內(nèi)存塊也不會(huì)被釋放掉,依然保持有效)!而如果只調(diào)用
一次delete,則使用new所分配的內(nèi)存就會(huì)被釋放掉.