PRODUCT : Borland C++ NUMBER : 1714 VERSION : All OS : All DATE : October 25, 1993 PAGE : 1/4 TITLE : Classes, memory models, and class modifiers This document discusses the relationship among classes, memory models, and class modifiers. Just as pointers can be near or far, one can declare classes using the following modifiers: near, far, huge, and _export. These modifiers affect four items of the class: the size of the "this" pointer, the size of the virtual function table pointer, the location of the virtual function table, and location of the static data members. For a given class, the following table summarizes the attributes of the above according to the memory model used: tptr = "this" pointer mfptr = member function pointer sdata = segment location of static data (far = far data) vptr = pointer to virtual function table vtbl = segment location of virtual function table tptr mfptr sdata vptr vtbl ---- ----- ----- ---- ---- Tiny near near _DATA near _DATA Small near near _DATA near _DATA Medium near far _DATA near _DATA Compact far near _DATA near _DATA Large far far _DATA near _DATA Huge far far fname_DATA far fname_DATA Optionally, one can use one of the four modifiers (near, far, huge, _export) when declaring classes. For example, class far foo { public: ... } These modifiers provide more control over the C++ class attributes mentioned above. The modifiers apply to all derivative classes and cannot be overridden. That is, in the example above, all classes that inherit from "foo" are automatically far classes, and any attempt to use other modifiers will generate a compiler error. The effects are listed in the following tables: PRODUCT : Borland C++ NUMBER : 1714 VERSION : All OS : All DATE : October 25, 1993 PAGE : 2/4 TITLE : Classes, memory models, and class modifiers For far classes: tptr mfptr sdata vptr vtbl ---- ----- ----- ---- ---- Tiny far near _DATA near _DATA Small far near _DATA near _DATA Medium far far _DATA near _DATA Compact far near _DATA near _DATA Large far far _DATA near _DATA Huge far far fname_DATA near fname_DATA For huge classes: tptr mfptr sdata vptr vtbl ---- ----- ----- ---- ---- Tiny far near far far _TEXT Small far near far far _TEXT Medium far far far far fname_TEXT Compact far near far far _TEXT Large far far far far fname_TEXT Huge far far far far fname_TEXT For _export classes: tptr mfptr sdata vptr vtbl ---- ----- ----- ---- ---- Tiny far far far far _TEXT Small far far far far _TEXT Medium far far far far fname_TEXT Compact far far far far _TEXT Large far far far far fname_TEXT Huge far far far far fname_TEXT For near classes: tptr mfptr sdata vptr vtbl ---- ----- ----- ---- ---- Tiny near near _DATA near _DATA Small near near _DATA near _DATA Medium near near _DATA near _DATA PRODUCT : Borland C++ NUMBER : 1714 VERSION : All OS : All DATE : October 25, 1993 PAGE : 3/4 TITLE : Classes, memory models, and class modifiers Applications ------------ For most programs, the programmer need not worry about these class modifiers. However, this knowledge is crucial if one wants to share classes between a DLL and an EXE for a Windows program. If a class needs to be shared, it must be instructed to use far (32-bit) pointers and far data segments, for its function and static data members, respectively. In the main EXE program, the class will need the "huge" modifier, since this will generate the correct types of pointers. Note that "huge" must be used instead of "far", as the latter does not generate the desired results. In the DLL, the class needs to be declared as "_export", which has the same effects as "huge", but also sets up the member functions to be exported / exportable. Doing so will ensure that the class will behave properly during program execution. A convenient way to declare the class properly is to use the following preprocessor macro: #ifdef __DLL__ # define EXPORT _export #else # define EXPORT huge #endif And then use the macro as in the following: class EXPORT foo { // == class _export foo, for DLLs public: // == class huge foo, for EXEs ... }; For more information, see also Chapter 8 of the Programmer's Guide about building Windows programs. Another application of the class modifiers is to save space in the data segment. In all memory models (except huge), the virtual function tables are placed in the _DATA segment. Since a table must be created for each class, the _DATA segment can PRODUCT : Borland C++ NUMBER : 1714 VERSION : All OS : All DATE : October 25, 1993 PAGE : 4/4 TITLE : Classes, memory models, and class modifiers quickly fill up when the number of classes increase. One option here would be to use the "huge" or "_export" modifiers, which will create the virtual function tables in the _TEXT (code) segment instead. Doing so will help avoid the "DGROUP exceeds 64K" error messages generated during compilation. For more information on segments, see TI703. 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.