#pragma variable

Category

Object code control

Purpose

Specifies whether the compiler is to use a named external object in a reentrant or non-reentrant fashion.

Syntax

Read syntax diagramSkip visual syntax diagram
>>-#--pragma--variable--(--identifier--, -+-rent---+--)--------><
                                          '-norent-'      

Defaults

C++ only. Variables are reentrant (rent). C++ only.

C only. Variables are not reentrant (norent). C only.

Parameters

identifier
The name of an external variable.
rent
Specifies that the variable's references or definition will be in the writable static area that is in modifiable storage.
norent
Specifies that the variable's references or its definition is in the code area and is in potentially read-only storage. This suboption does not apply to, and has no effect on, program variables with static storage class.

Usage

If an identifier is defined in one translation unit and used in another, the reentrant or non-reentrant status of the variable must be the same in all translation units.

C only. To specify that variables declared as const not be placed into the writeable static area, you must use the ROCONST and RENT compiler options. C only.

If the specification for a const variable in a #pragma variable directive conflicts with the ROCONST option, the pragma directive takes precedence over the compiler option, and the compiler issues an informational message.

If you use the norent suboption for a variable, ensure that your program never writes to this variable. Program exceptions or unpredictable program behavior may result should this be the case.

The following code fragment leads to undefined behavior when compiled with the RENT option.
int i;
int *p = &i;
#pragma variable(p, norent)
The variable i is reentrant, but the pointer p is non-reentrant. If the code is in a DLL, there will only be one copy of p but multiple copies of i, one for each caller of the DLL.

A non-reentrant pointer variable cannot take an address as an initializer: the compiler will treat the variable as reentrant if necessary (in other words, it will ignore the pragma). Initializers for non-reentrant variables should be compile-time constants. Due to code relocation during execution time, an address in a program that has both reentrant and non-reentrant variables is never considered a compile-time constant. This restriction includes the addresses of string literals.

Examples

The following program fragment shows how to use the #pragma variable directive to force an external program variable to be part of a program that includes executable code and constant data.
#pragma variable(rates, norent)
extern float rates[5] = { 3.2, 83.3, 13.4, 3.6, 5.0 };

extern float totals[5];

int main(void) {
⋮
}
In this example, you compile the source file with the RENT option. The executable code includes the variable rates because #pragma variable(rates, norent) is specified. The writeable static area includes the variable totals. Each user has a personal copy of the array totals, and all users of the program share the array rates. This sharing may yield a performance and storage benefit.

Related information