Foundation Class Collections Limited to 32K Total Objects

Last reviewed: July 17, 1997
Article ID: Q85515
7.00 | 1.00 1.50 1.51 1.52 MS-DOS | WINDOWS kbprg

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft C/C++ version 7.0
        - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51,
          and 1.52
    

SUMMARY

Each object of the "collection" classes provided by the Microsoft Foundation Classes can store no more than 32,767 objects.

Collection classes include the following:

   Arrays          Lists           Maps
   ----------------------------------------------------

   CByteArray      CObList         CMapPtrToWord
   CDWordArray     CPtrList        CMapPtrToPtr
   CObArray        CStringList     CMapStringToOb
   CPtrArray                       CMapStringToOb
   CStringArray                    CMapStringToString
   CWordArray                      CMapWordToOb
                                   CMapWordToPtr

MORE INFORMATION

Each of the Microsoft Foundation Classes collection classes uses a signed integers to store the total number of objects in an object of the class. In MS-DOS and 16-bit Windows, a signed integer is limited to 32K. In Windows NT, a 32-bit signed integer can index up to 2 billion items.

If you need to store more than 32K objects in a collection object, you can create a collection class that does not use an "int" to index the array.

The TEMPLDEF sample program includes a sample "list" implementation, which may be modified to implement a collection of more than 32K objects. Changing the type of the list's index variable from an "int" to a "long" allows an application to create a list that contains up to 2,048,000 objects.

To implement the modified "list" class, perform the following six steps:

  1. Build the TEMPLDEF sample application in the Microsoft Foundation Classes Samples directory.

  2. Copy the LIST.CTT template to LLIST.CTT. This file will contain the new "list" class.

  3. Make the following five changes to LLIST.CTT:

    a. Change all occurrences of CList to CLongList. CLongList is the

          base for the new list class.
    

    b. Change the line

             int  GetCount() const
    
          to the following:
    
             long GetCount() const
    
          and change
    
             _AFXCOLL_INLINE int CLongList<TYPE, ARG_TYPE, IS_SERIAL,
             HAS_CREATE>::GetCount() const
    
          to the following:
    
             _AFXCOLL_INLINE long CLongList<TYPE, ARG_TYPE, IS_SERIAL,
             HAS_CREATE>::GetCount() const
    
       c. Change the line
    
             POSITION FindIndex(int nIndex) const;
    
          to the following:
    
             POSITION FindIndex(long nIndex) const;
    
       d. Change the line
    
             int     m_nCount;
    
          to the following:
    
             long    m_nCount;
    
       e. Change the line
    
             POSITION CLongList<TYPE, ARG_TYPE, IS_SERIAL,
               HAS_CREATE>::FindIndex(int nIndex) const
    
          to the following:
    
             POSITION CLongList<TYPE, ARG_TYPE, IS_SERIAL,
               HAS_CREATE>::FindIndex(long nIndex) const
    
    

  4. Create the LLIST_O.H and LLIST_O.CPP files using the following command:

          templdef "CLongList<CObject*,CObject*,0,0> CObLongList"
    
              llist.ctt llist_o.h llist_o.inl llist_o.cpp
    
       Make sure that LLIST_O.H is included in LLIST_O.CPP.
    
    

  5. Copy the PLEX.H file from the Microsoft Foundation Classes Source (SRC) directory to the Microsoft Foundation Classes INCLUDE directory.

  6. Add the following line to the LLIST_O.H file:

          // Inline function declarations
          #ifdef _AFX_ENABLE_INLINES
          #include "llist_o.inl"
          #endif //_AFX_ENABLE_INLINES
    

  7. Add the following line to the LLIST_O.CPP file:

          // Inlines
          #ifndef _AFX_ENABLE_INLINES
          #define _AFXCOLL_INLINE
          #include "llist_o.inl"
          #endif //!_AFX_ENABLE_INLINES
    

The LLIST_O.CPP and LLIST_O.H files contain the code to implement the CLongList class. A CLongList object can contain up to 2,048K CObject- type objects. To use this class, compile the LLIST_O.CPP file and link the LLIST_O.OBJ file with your source code.

NOTE: You cannot serialize a CLongList object because serialization does not support more than 32K objects in an instance of a class.

The TEMPLDEF sample program also provides the source code for ARRAY and MAP classes in the ARRAY.CTT and MAP.CTT files, respectively. You can modify these files as above to create map classes that support more than 32K elements.

The ARRAY class has an additional limitation. The CObArray class implements an "array" of pointers to CObjects or objects derived from CObject. This array, and about 100 bytes of overhead, must reside in one 64K segment. Therefore, the array is limited to approximately 32,000 elements if the CObject pointers are 16-bit near pointers (as appropriate for the small or medium memory models) or 16,000 elements if the pointers are 32-bit far pointers (as appropriate for the compact and large memory models).


Additional reference words: kbinf 7.00 1.00 1.50 2.00 2.50 2.51 2.52
KBCategory: kbprg kbfasttip
KBSubcategory: MfcMisc
Keywords : kb16bitonly
Technology : kbMfc


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 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.