Why GlobalAlloc() Returns a Handle to a 0-Byte Segment

Last reviewed: July 22, 1997
Article ID: Q61777
3.00 3.10 WINDOWS kbprg

The information in this article applies to:

  • Microsoft Windows Software Development Kit (SDK) for Windows versions 3.0 and 3.1

When Calling GlobalAlloc() with a segment of size 0 (zero), Windows returns a handle to the memory. Attempting a GlobalLock() using that handle returns a NULL pointer. Since a 0-byte block of memory is treated by the system as if it is discarded, the following explains how these memory handles can be reused.

Consider the following scenario:

  1. hMemory = GlobalAlloc (GMEM_MOVEABLE, 0L);

    This returns a handle to a block of memory of length 0 (zero).

  2. wFlags = GlobalFlags (hMemory);

    wFlags is 0x4000, which is GMEM_DISCARDED. Thus, the memory object is thought of as if it has already been discarded.

  3. lpszMemory = GlobalLock (hMemory);

    This fails. lpszMemory == 0.

  4. hMemory = GlobalReAlloc (hMemory, 1L, GMEM_MOVEABLE);

    This returns the handle, and sets the size of the block to 1 byte.

  5. wFlags = GlobalFlags (hMemory);

    wFlags = 0 (zero), which is GMEM_FIXED (Windows protected mode or in Windows 3.1, GMEM_FIXED and GMEM_MOVEABLE are the same).

  6. lpszMemory = GlobalLock (hMemory);

    This returns a valid far pointer to a 1-byte buffer.

When the lock count of a GMEM_DISCARDABLE object reaches 0 (zero), it is a candidate for discarding. If it has been discarded, it cannot be locked or used because the memory it refers to has been deallocated. However, it can be reallocated and then locked. As a result, the memory handles are reused after having been discarded.


Additional reference words: 3.00 3.10
KBCategory: kbprg
KBSubcategory: KrMm
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.