GlobalAlloc鏄爣鍑嗗唴瀛樼鐞嗗嚱鏁幫紝鏍囧噯鍐呭瓨綆$悊鍑芥暟閮芥槸鎿嶄綔榪涚▼鐨勯粯璁ゅ爢錛屾墍浠ヨ繖涓嚱鏁版槸浠庤繘紼嬬殑榛樿鍫嗕腑鍒嗛厤鍐呭瓨絀洪棿錛屽垎閰嶇殑絀洪棿鍙互鏄彲縐誨姩鐨勪篃鍙互鏄笉鍙Щ鍔ㄧ殑銆傚彲縐誨姩鐨勫唴瀛樻槸鎸嘩indows鍦ㄩ渶瑕佺殑鏃跺欏彲浠ュ皢榪欎釜鍐呭瓨縐誨姩鍒板彟澶栦竴涓湴鍧.
鍏充簬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鏄爢綆$悊鍑芥暟錛屽爢綆$悊鍑芥暟鍙互鎿嶄綔闈為粯璁ゅ爢錛堝綋鐒朵篃鍙互鎿嶄綔榛樿鍫嗭級錛屽垱寤轟竴涓爢鏄敤HeapCreate錛岃繖涓嚱鏁拌繑鍥炰竴涓爢鍙?
鏌勶紝鐒跺悗鍙互鐢ㄥ湪HeapAllock鍑芥暟涓紝鍗充粠榪斿洖鐨勮繖涓爢涓敵璇峰唴瀛樼┖闂達紝HeapAllock鐢寵鐨勫唴瀛樺彧鑳芥槸涓嶅彲浠ョЩ鍔ㄧ殑.
鑰宯ew鍒欐槸c++鐨勬爣鍑嗗嚱鏁幫紝鍦╓indows鐨刅C++緙栬瘧鍣ㄤ腑錛宯ew鍦ㄧ敵璇峰唴瀛樻椂鏈緇堣皟鐢ㄧ殑鏄疓labalAlloc錛屼笉榪噉ew榪樺彲浠ヨ皟鐢ㄧ被鐨勬瀯閫犲嚱鏁?
Windows鐨勫唴瀛樼鐞嗛櫎浜嗘爣鍑嗗唴瀛樼鐞嗗嚱鏁板拰鍫嗙鐞嗗嚱鏁頒箣澶栵紝榪樻湁鏇村姞搴曞眰鐨勮櫄鎷熷唴瀛樼鐞嗗嚱鏁幫紝VirtualAlloc灝辨槸涓涓櫄鎷熷唴瀛樼鐞嗗嚱鏁?
Personal Comprehension
GlobalAlloc鍒嗛厤鐨勫唴瀛?榪樺彲浠ヨ皟鐢℅lobalLock閿佸畾璇ュ唴瀛樺潡(璇ュ嚱鏁板彲浠ヨ澶氭璋冪敤),鍦ㄦ垜浠病鏈夎皟鐢℅lobalUnlock涔?
鍓?璇ュ唴瀛樺潡浼氫竴鐩翠繚鎸佹湁鏁?鍗充嬌璋冪敤浜咷lobalFree鍑芥暟,浣嗗鏋滆鍐呭瓨鐨勯攣璁℃暟涓嶄負0,璇ュ唴瀛樺潡涔熶笉浼氳閲婃斁鎺?渚濈劧淇濇寔鏈夋晥)!鑰屽鏋滃彧璋冪敤
涓嬈elete,鍒欎嬌鐢╪ew鎵鍒嗛厤鐨勫唴瀛樺氨浼氳閲婃斁鎺?

]]>