### #pragma data_seg (OS/2, Windows)

The #pragma data_seg directive lets you place static and external variables in different 32-bit data segments.

All static and external variables defined after the #pragma data_seg directive are placed in the named data_segment. The pragma is in effect until the next #pragma data_seg directive or the end of the compilation unit.

Restrictions:

- Writable string literals used to initialize pointers are not placed in the named data_segment, but in the default 32-bit data segment (DATA32). To place a string in a particular data segment, use an array to initialize the string instead of a pointer. For example:

         char a[ ] = "mystring";

  instead of

         char *a = "mystring";

- #pragma data_seg applies only to 32-bit data segments. Data placed in 16-bit segments because of #pragma seg16 are not affected by #pragma data_seg, and are placed in 16-bit data segments.

Static and external variables defined before the first #pragma data_seg directive are placed in the default DATA32 segment, with the exception of uninitialized variables and variables explicitly initialized to zero, which are placed in the BSS32 segment. You can also use # pragma data_seg to explicitly place variables in the DATA32 segment by specifying no data_segment, for example, #pragma data_seg(). However, you cannot use the CONST32_RO or BSS32 segments in a #pragma data_seg directive.

Note: Because the variables in the BSS32 data segment are initialized at load time and loaded separately from the rest of your program, they take less space in your executable file. If you place these variables in a different data segment, this optimization does not take place, and the size of your executable module increases. For this reason, if the size of your executable file is critical to you, you should define all variables initialized to zero (either explicitly or by default) before the first occurrence of #pragma data_seg.

Defining your own data segments allows you to group data depending on how it is used and to specify different attributes for different groups of data, such as when the data should be loaded. You specify attributes for data segments in a module definition (.DEF) file.

For example, when you build a DLL, you might have one set of data that you want to make global for all processes that use the DLL, and a different set of data that you want to copy for each process. You could use #pragma data_seg to place the data in two different segments as follows:

       #pragma data_seg(globdata)

       static int counter1 = 0;
       static int counter2 = 0;
       .
       .
       .
       #pragma data_seg(instdata)

       static int instcount1 = 0;
       static int instcount2 = 0;
       .
       .
       .

You could then place the following statements in the program's .DEF file:

       SEGMENTS
          globdata CLASS 'DATA' SHARED
          instdata CLASS 'DATA' NONSHARED

SHARED specifies that the data in the globdata segment is global or shared by all processes that use the DLL. NONSHARED means that each process gets its own copy of the data in the instdata segment.