PRODUCT : Borland C++ NUMBER : 863 VERSION : 3.x OS : WIN DATE : October 19, 1993 PAGE : 1/2 TITLE : Understanding Memory Allocation Under Windows This document is a brief summary of how functions such as malloc, calloc, and new allocate memory in a Windows application created with Borland C++ 3.x In all cases, new calls malloc to allocate the memory. What happens next depends on the memory model your in and whether your building a Dynamic Link Library (.DLL) or an Exectuable (.EXE). If you are in the small or medium memory model building an .EXE file, where data pointers default to near... new calls malloc which calls LocalAlloc( LMEM_FIXED ); calloc calls LocalAlloc( LMEM_FIXED | LMEM_ZEROINIT ); If you are in the compact or large model building an .EXE file, where data pointers default to far, malloc calls farmalloc... new calls malloc which calls farmalloc which calls GlobalLock( GlobalAlloc( GMEM_MOVEABLE | _WinAllocFlag ) ); calloc calls farcalloc which ultimately make a call to GlobalLock( GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT | _WinAllocFlag ); If you are in any memory model (Small, Medium, Compact, or Large) building a .DLL file, all data pointers, by default, are far and the procedure used by the Runtime Library to allocate memory is exactly the same as a large model .EXE. Large model .EXE files are discussed in the previous paragraph. _WinAllocFlag is a WORD in the startup code which is always 0. One could set it to GMEM_SHARE or whatever other flags are needed before doing a new or malloc. To use it, first make an extern for it in your program... extern WORD _WinAllocFlag; Some additional Notes, in Borland C++ 3.x, and in the Object Windows Library for Borland C++ 2.0, Borland has implemented a suballocation scheme for use with global allocations. This means memory is allocated as discussed before, but if a memory request is small, a pointer will be returned into a larger chunk. This reduces the amount of selectors Windows has to allocate. PRODUCT : Borland C++ NUMBER : 863 VERSION : 3.x OS : WIN DATE : October 19, 1993 PAGE : 2/2 TITLE : Understanding Memory Allocation Under Windows In Borland C++ 3.x, the size of the chunks the runtime library allocates are always 4K of RAM. If you ask for more than 4K, the request for memory goes straight to GlobalAlloc, and you can expect an offset of 0 for your pointer. In Borland C++ 3.x, if you link to the static libraries and you set _WinAllocFlag to GMEM_SHARE before doing an allocation, we do not use the sub allocator, your memory request maps directly to GlobalAlloc. This is particular important when allocating memory in a DLL which provides services to two or more applications. By default memory allocated in a DLL belongs to the task ( .EXE ) which called the DLL. The memory is automatically freed if the task terminates without releasing the memory. If a DLL used by more than one tasks allocates memory, the memory should not be suballocated since depending on the order the tasks terminates, memory still in use by the DLL and another task could be freed when the task owning the memory terminates. Using the GMEM_SHARE flag results in the memory being owned by the DLL. DISCLAIMER: You have the right to use this technical information subject to the terms of the No-Nonsense License Statement that you received with the Borland product to which this information pertains.