Page 95 - DCAP408_WEB_PROGRAMMING
P. 95
Unit 5: Memory Management (I)
Notes
Task Make distinction between uBytes and uFlags.
5.5.2 Return Values
If the function succeeds, the return value is the handle of the reallocated memory object.
If the function fails, the return value is NULL. To obtain extended error information, call
GetLastError.
Notes If LocalReAlloc reallocates a movable object, the return value is the handle of the
memory object. To translate the handle to a pointer, make use of the LocalLock function.
If LocalReAlloc reallocates a fixed object, the value of the handle returned is the address of
the first byte of the memory block. To access the memory, a process can just cast the return
value to a pointer.
Self Assessment
Fill in the blanks:
11. The .................... function modifies the size or the attributes of a stated local memory
object.
12. .................... determines the local memory object to be reallocated.
13. .................... specifies how to reallocate the local memory object.
5.6 Discardable Memory Blocks
Windows can reallocate a discardable memory block to a 0 length when it requires space to
gratify another allotment request. Performing this destroys all data enclosed in the memory
block. You usually use a discardable memory block to hold information that is suitable to keep
in memory but that can simply be recreated when required.
You cannot obtain a discardable memory block by means of malloc or HeapAlloc.
!
Caution You must either use GlobalAlloc with the GMEM_MOVEABLE and
GMEM_DISCARDABLE flags.
When you assign a discardable memory block, Windows returns a handle to the block. When
you lock the block, Windows returns the address of the block. The handle returned when allocating
a discardable memory block remains suitable even after the block is discarded; this can happen
at any time the block is unlocked.
The function call in this case turns out to be
HANDLE hMem;
hMem=GlobalAlloc(GMEM_MOVEABLE|GMEM_DISCARDABLE,10);
LOVELY PROFESSIONAL UNIVERSITY 89