Global and local memory management transcoding: About memory Functions GlobalLock (), GlobalAlloc (), GlobalUnlock () usage
1. Why use the GlobalLock () function
C + + code
Hglobal Himagememory=globalalloc (gmem_moveable, dwfilesize); Assigning global memory to a picture
void *pimagememory=globallock (himagememory); Lock memory
DWORD dwreadedsize; Save the actual read file size
ReadFile (hfile, Pimagememory, dwFileSize, &dwreadedsize, NULL); Reading images into global memory
GlobalUnlock (himagememory); Unlocking memory
CloseHandle (hfile); Close file handle
IStream *pistream;//Create a IStream interface pointer to save the picture stream
Answer:
Lobalalloc apply for two kinds of memory, one is gmem_fixed, the other is gmem_moveable.
The difference between the two is that the gmem_moveable type of memory operating system can be moved, such as the heap has several pieces of small memory,
When another chunk of memory is requested, the operating system moves the gmem_moveable type of memory to merge a chunk.
Because the gmem_moveable is movable, it is identified with a handle and cannot be identified with a memory address.
The memory address is obtained by the handle by GlobalLock when used.
For the gmem_fixed type, the handle returned by the function is a memory pointer that can be used directly when the memory pointer is in use.
Source: http://topic.csdn.net/u/20100802/17/2e66b3ef-285d-43da-b5a2-60f8d0665fbd.html
2. VC in the use of globalalloc,globallock,globalunlock and questions
Call the GlobalAlloc function to allocate a chunk of memory, which returns the allocated memory handle.
Call the GlobalLock function to lock the memory block, which takes a memory handle as a parameter, and then returns a pointer to the block of memory that is locked. You can use this pointer to read and write memory.
Call the GlobalUnlock function to unlock previously locked memory, which invalidates pointers to memory blocks.
Call the GlobalFree function to free the memory block. You must pass a memory handle to the function.
GlobalAlloc
Description
Allocating a global block of memory
return value
A Long that returns a global memory handle. Zero indicates failure. Will set GetLastError
Parameter table
Parameter type and description
Wflags Long, a constant flag defined for the allocated memory type, as follows:
Gmem_fixed allocating a fixed block of memory
Gmem_moveable allocating a removable block of memory
Gmem_discardable allocating a memory block that can be discarded
The Gmem_nocompact heap does not accumulate during this function call
No memory block is discarded during Gmem_nodiscard function call
Gmem_zeroinit all newly allocated memory blocks are initialized to 0
Dwbytes Long, the number of characters to allocate
Annotations
If gmem_fixed is specified, the return value is the actual memory address that is to be used (GlobalLock returns the same value)-so there is no need to perform a globallock/globalunlock operation when using a fixed block of memory
Because Win32 uses a high-level memory management scheme, there is no benefit in using removable memory blocks
Memory blocks allocated with this function are allowed within 8-bit boundaries
"Attached" questions about GlobalAlloc
--------------------------------------------------------------------------------
Q: When allocating a global memory block using GlobalAlloc, what is the difference between allocating a fixed block of memory using gmem_fixed and allocating a removable block of memory using gmem_moveable? (Please specify the point)
Is there any difference in efficiency?
Why in some source code, and then use the GMEM_MOVEABLE flag to allocate memory, will use GlobalFree to its returned memory handle for the release operation of the statement commented out, or simply do not write? Don't you need to do that?
--------------------------------------------------------------------------------
A: Gmem_moveable is to allow the operating system (or application) to implement the management of the memory heap, if necessary, the operating system can move the memory block to obtain a larger block, or merge some free memory blocks, also known as "garbage collection", it can improve the utilization of memory. In general, the memory heap space is managed by the user, and the Windows operating system does not intervene. If the following is the case where there are 10 free blocks of 1K in the heap, then if you request a 5K memory space directly, you will get unsuccessful information. However, if other memory blocks that are already occupied are movable, then the system can move the memory blocks, merge a 5k block of memory, and successfully assign it to the user. Its spatial efficiency is at the cost of time efficiency at runtime.
GlobalLock
Function Description: Locks a global memory object, returning a pointer to the first byte of the object
Function Prototypes:
LPVOID GlobalLock (Hglobal hmem)
Parameters:
Hmem: A handle to the global memory object. This handle is obtained by GlobalAlloc or GlobalRealloc.
return value:
The call succeeds, returning a pointer to the first byte of the object
Call failed, return NULL, can use GetLastError to get error message
Attention:
After calling GlobalLock to lock a chunk of memory, be sure to call GlobalUnlock to unlock it.
GlobalUnlock
function function Description: Unlock global memory objects that are locked
Function prototype: BOOL GlobalUnlock (Hglobal hmem);
Parameter: Hmem: Handle to Global Memory object
return value:
Non-0 value, the specified memory object is still in the locked state
0, function execution error, can use GetLastError to get error message, if return no_error, it means that the memory object has been unlocked
Note: This function is actually to reduce the memory object's lock counter one, if the counter is not 0, it means to execute too many GlobalLock functions to lock the memory object, need the corresponding number of globalunlock function to unlock.
If the error code returned by the GetLastError function is error_not_locked, it is either unlocked or unlocked.
Example:
Malloc Memory
Hmem = GlobalAlloc (gmem_moveable | Gmem_ddeshare, nSize);
Lock Memory
Pmem = (BYTE *) GlobalLock (HMEM);
..................
Unlock Memory
GlobalUnlock (HMEM);
1. HeapAlloc:
The explanation on MSDN is that HeapAlloc is allocating a piece of memory from the heap, and the allocated memory is not movable (that is, if there is no contiguous space to accommodate the allocated size, the program cannot exploit the other scattered space, resulting in an allocation failure), which is allocated from a specified address. Unlike Gloabalalloc, which is distributed from the global heap, this could be global or local. The function prototypes are:
LPVoid
HeapAlloc (
HANDLE Hheap,
DWORD DwFlags,
size_t dwbytes
);
Hheap is the start location of the process heap memory.
Dwflags is a flag that allocates heap memory. Including Heap_zero_memory, even if the allocated space is zeroed.
Dwbytes is the size of the allocated heap memory.
Its corresponding free space function is heapfree.
2. GlobalAllocThis function is used to allocate memory from the global heap for program use, and the function prototype is:
Hglobal GlobalAlloc (
UINT Uflags,
size_t dwbytes
);
Uflags parameter meaning
Combination of ghnd gmem_moveable and Gmem_zeroinit
gmem_fixed allocates fixed memory, the return value is a pointer
Gmem_moveable allocates active memory, in Win32, memory blocks cannot be moved in physical memory, but can be moved in the default heap. The return value is a handle to the memory object, and the handle can be converted to a pointer using the function GlobalLock
Gmem_zeroinit Initialize memory contents to zero
Combination of gptr gmem_fixed and Gmem_zeroinit
In general, when we are programming, the memory allocated to the application is movable or can be discarded, so that the limited memory resources can be fully utilized, so at some point we allocate the memory of the address is indeterminate, because he can be moved, so we have to lock the block of memory first, Here the application needs to call the API function GlobalLock function to lock the handle. as follows: Lpmem=globallock (HMEM); This allows the application to access this memory. So when we use globalallock, we usually use the GlobalLock, of course, when we do not use memory, we must remember to use GlobalUnlock, otherwise the locked memory block can not be used by other variables.
The GlobalAlloc corresponds to a function that frees space for GlobalFree.
3. LocalAllocThis function is used to allocate memory from the local heap for use by the program, and the function prototype is:
Hlocal LocalAlloc (
UINT Uflags,
size_t ubytes
);
Parameters are the same as GlobalAlloc.
There is a difference in 16-bit Windows because 16-bit Windows uses a global heap and a local heap to manage memory, and when each application or DLL is loaded into memory, the code snippet is loaded into the global heap, and the system allocates a 64KB data segment from the global heap for each instance as the local heap of that instance That holds the stack and all global or static variables for the application. The Localalloc/globalalloc is used to allocate memory in the local heap or the global heap, respectively.
Because the local heap of each process is small, allocating memory in the local heap is constrained by space. But this heap is private to each process, relatively safe to allocate data, and data access errors do not affect the entire system.
The memory allocated in the global heap is shared for each process, and each process can access the memory as long as the handle that owns the block of memory is available, but each global memory space requires additional memory overhead, resulting in wasted allocation. And once a serious error occurs, it may affect the stability of the system as a whole.
In Win32, however, each process has only one private heap that is missing, and it can only be accessed by the current process. Applications also cannot directly access system memory. So in Win32, both the global heap and the local heap point to the process's default heap. There is no difference in allocating memory with Localalloc/globalalloc. Even LocalAlloc allocated memory can be freed by GlobalFree. So programming under Win32, without noticing the difference between local and global, the general memory allocation is equivalent to HeapAlloc (GetProcessHeap (),...).
The LocalAlloc corresponding release function is Lockfree.
4. VirtualAllocThe function's function is to call the process's virtual address space, book or submit a portion of the page, if used for memory allocation, and the allocation type does not specify Mem_reset, the system will be automatically set to 0; its function prototype:
LPVOID VirtualAlloc (
LPVOID lpaddress,//region to reserve or commit
size_t dwsize,//size of Region
DWORD Flallocationtype,//type of allocation
DWORD flprotect//type of Access Protection
);
VirtualAlloc can persist a large area of memory by committing part or all of a region in parallel multiple invocations. Multiple invocations that commit the same area do not cause a failure. This allows an application to leave the memory free to submit pages that will be written. When this method is not valid, it frees the application to see if it has been committed before committing the call by detecting the state of the page being retained.
The VirtualAlloc corresponding release function is VirtualFree.
5. Mallocmalloc and free are standard library functions for C++/C languages, which can be used to request dynamic memory and release memory. For objects of non-intrinsic data types, light Malloc/free cannot meet the requirements of dynamic objects. Objects are automatically executed when they are created, and the object executes the destructor automatically before it dies. Because Malloc/free is a library function and not an operator, the task of executing constructors and destructors cannot be imposed on malloc/free, not within the control of the compiler.
6. NewNew/delete is an operator of C + +. Can be used to request dynamic memory and free memory. The C + + language requires an operator new that can perform dynamic memory allocation and initialization, with an operator delete that can perform cleanup and release of memory work. Note New/delete is not a library function. C + + programs often have to invoke the function, whereas C programs can only use Malloc/free to manage dynamic memory. New is an operator, and what "+", "-", "=" ... Have the same status.
Malloc is a function that allocates memory for you to invoke.
New is a reserved word and does not require header file support.
malloc requires the support of the header vault function. New is an object that is created
malloc allocates a piece of memory.
New object You can use it as an ordinary object, access it with a member function, and do not directly access its address space.
Malloc allocates a chunk of memory, accesses it with a pointer, and can move the pointer inside.
A memory leak can be checked for malloc or new, except that new can indicate the row of that file, and malloc does not have that information. New can be thought of as the execution of malloc plus constructors. The new pointer is directly with the type information. and malloc returns a void pointer.
Global and local memory management