Foundation Class Collections Limited to 32K Total Objects

ID: Q85515


The information in this article applies to:


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 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:

    1. Change all occurrences of CList to CLongList. CLongList is the base for the new list class.


    2. 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 


    3. Change the line
      
               POSITION FindIndex(int nIndex) const; 
      to the following:
      
               POSITION FindIndex(long nIndex) const; 


    4. Change the line
      
               int     m_nCount; 
      to the following:
      
               long    m_nCount; 


    5. 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 query words: kbinf 7.00 1.00 1.50 2.00 2.50 2.51 2.52


Keywords          : kb16bitonly 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: August 3, 1999