Microsoft KB Archive/85515

{|
 * width="100%"|

Foundation Class Collections Limited to 32K Total Objects

 * }

Q85515

-

The information in this article applies to:


 * The Microsoft Foundation Classes (MFC), included with:
 * Microsoft C/C++ for MS-DOS, version 7.0
 * Microsoft Visual C++, versions 1.0, 1.5, 1.51, 1.52

-

SUMMARY
Each object of the &quot;collection&quot; 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 &quot;int&quot; to index the array.

The TEMPLDEF sample program includes a sample &quot;list&quot; 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 &quot;int&quot; to a &quot;long&quot; allows an application to create a list that contains up to 2,048,000 objects.

To implement the modified &quot;list&quot; class, perform the following steps:

 Build the TEMPLDEF sample application in the Microsoft Foundation Classes Samples directory. Copy the LIST.CTT template to LLIST.CTT. This file will contain the new &quot;list&quot; class. Make the following five changes to LLIST.CTT:

 Change all occurrences of CList to CLongList. CLongList is the base for the new list class.  Change the line         int  GetCount const to the following:          long GetCount const and change         _AFXCOLL_INLINE int CLongList::GetCount const to the following:          _AFXCOLL_INLINE long CLongList<TYPE, ARG_TYPE, IS_SERIAL, HAS_CREATE>::GetCount const </li>  Change the line <pre class="CODESAMP">        POSITION FindIndex(int nIndex) const; to the following:  <pre class="CODESAMP">        POSITION FindIndex(long nIndex) const; </li>  Change the line <pre class="CODESAMP">        int     m_nCount; to the following:  <pre class="CODESAMP">        long    m_nCount; </li>  Change the line <pre class="CODESAMP">        POSITION CLongList<TYPE, ARG_TYPE, IS_SERIAL, HAS_CREATE>::FindIndex(int nIndex) const to the following:  <pre class="CODESAMP">        POSITION CLongList<TYPE, ARG_TYPE, IS_SERIAL, HAS_CREATE>::FindIndex(long nIndex) const </li></ol> </li>  Create the LLIST_O.H and LLIST_O.CPP files using the following command:  <pre class="CODESAMP">     templdef &quot;CLongList<CObject*,CObject*,0,0> CObLongList&quot; llist.ctt llist_o.h llist_o.inl llist_o.cpp Make sure that LLIST_O.H is included in LLIST_O.CPP. </li> Copy the PLEX.H file from the Microsoft Foundation Classes Source (SRC) directory to the Microsoft Foundation Classes INCLUDE directory.</li>  Add the following line to the LLIST_O.H file:  <pre class="CODESAMP">     // Inline function declarations #ifdef _AFX_ENABLE_INLINES #include &quot;llist_o.inl&quot; #endif //_AFX_ENABLE_INLINES </li>  Add the following line to the LLIST_O.CPP file:  <pre class="CODESAMP">     // Inlines #ifndef _AFX_ENABLE_INLINES #define _AFXCOLL_INLINE #include &quot;llist_o.inl&quot; #endif //!_AFX_ENABLE_INLINES </li></ol>

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 &quot;array&quot; 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

Issue type :

Technology : kbAudDeveloper kbMFC