Because a 16-bit program cannot access a data item that is larger than 64K in size or that spans a 64K boundary in memory, any data items that are to be shared between 16-bit and 32-bit programs must conform to these limits. There are two ways to accomplish this:
#pragma seg16 Preprocessor Directive
Use #pragma
seg16 to ensure that shared data items do not cross 64K
boundaries. In most cases, you need only use this #pragma
directive with items that are likely to cross 64K boundaries,
such as aggregates, doubles, and long doubles.
You can use #pragma seg16 either with the data item directly or through a typedef. The following code fragment shows both ways of using #pragma seg16:
struct family {
long john;
double carolynn;
char * _Seg16 geoff;
long colleen;
};
#pragma seg16( cat )
struct family cat; /* cat is qualified directly */
typedef struct family tom; (1)
#pragma seg16( tom ) (2)
tom edna; /* edna is qualified using a typedef */ (3)
Note: Using #pragma seg16 on variables of type struct family does not mean that pointers inside the structure will automatically be qualified with _Seg16. If you want the pointers to be qualified as such, you must declare them yourself.
The #pragma seg16 directive can be used either before or after the variable or typedef name is declared. In the case of the typedef, however, the #pragma must be attached to the typedef name before that name is used in another declaration. For example, in the preceding example, lines (1) and (2) can appear in any order, but both must appear before line (3).
The #pragma seg16 directive cannot be used on objects greater than 64K.
tiledmemory Code Generation Option
The gen(tiledmemory)
option enables data to be shared between 32-bit and 16-bit
code. When you compile a program with gen(tiledmemory,yes) an
implicit #pragma seg16 directive is performed for all variable
declarations. Pointers are not implicitly qualified with _Seg16;
you must qualify them if desired.
The tiledmemory option also defines special versions of the malloc family of functions that return memory that can be safely used by 16-bit code. When tiledmemory is specified, all calls to calloc, malloc, realloc, and free are mapped to _tcalloc, _tmalloc, _trealloc, and _tfree respectively.
These functions work exactly like the original functions, but the memory allocated or freed will not cross 64K boundaries. The objects declared can be used in 16-bit programs. This memory is also called tiled memory and is limited to 512M per process.
Note: When you use the tiledmemory option, data items larger than 64K in size will be aligned on 64K boundaries, but will also cross 64K boundaries.
![]()
![]()
Calling Between 32-bit and 16-bit
Code
![]()
![]()
Declare Segmented Pointers
![]()
Pass Data Between 32-Bit and 16-Bit Code
