MFC Collections Limited to 32K Total Objects

ID Number: Q85515

1.00 | 1.00

MS-DOS | WINDOWS

Summary:

The "collection" classes included in the Microsoft Foundation Classes

(MFC) version 1.0 that shipped with the Microsoft C/C++ Compiler

version 7.0 are each limited to storing no more than 32,767 objects in

a collection.

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:

All of the MFC collection classes use signed integers to store the

total number of objects in a class. A signed integer is limited to

32K.

If more than 32K objects are needed in a collection, a user-defined

collection class may be written that does not use "int" to index the

array.

The TEMPLDEF sample program comes with a sample "list" implementation,

which may be modified to implement a collection greater than 32K.

Changing the list's index from an "int" to a "long" will allow a list

containing up to 2,048,000 objects to be created.

The following instructions describe what changes must be made to

implement this new "list" class:

1. Build the templdef sample application in the MFC samples directory.

2. Copy the LIST.CTT template to LLIST.CTT. This will contain the new

"list" class.

3. Make the following changes to LLIST.CTT:

a. Change all occurrences of CList to CLongList. CLongList will be

the base for the new list class.

b. Change the line

int GetCount() const

to the following:

long 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. Make 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.cpp

5. Copy the file PLEX.H from the ..\MFC\SRC directory to the

..\MFC\INCLUDE directory.

6. Add the line

#include "llist_o.h"

to the LLIST_O.CPP file.

The LLIST_O.H and LLIST_O.CPP files contain the code that implements

the CLongList class. This is a list class that can contain a maximum

of 2,048K CObjects. To use this class, compile the file LLIST_O.CPP

and link the resulting .OBJ file with your code. Please note that this

list class cannot be serialized, because serialization does not permit

more than 32K objects in an instance of a class.

The TEMPLDEF sample program also provides sample source for ARRAY and

MAP classes in the files ARRAY.CTT and MAP.CTT, respectively.

Modifications similar to the ones described above may be made to the

sample MAP class to create a new MAP implementation that contains more

than 32K elements.

The ARRAY class has an additional limitation, however. The CObArray

class implements an "array" of pointers to CObjects or objects derived

from CObject. This array must fit into a single 64K segment together

with approximately 100 bytes of overhead. Because of this limitation,

the array size is approximately 32,000 elements if the CObject

pointers are 16-bit near pointers (for example, small and medium

memory models), and 16,000 elements if the pointers are 32-bit far

pointers (for example, compact and large memory models).

Additional reference words: 7.00 1.00