Edit

Share via


Compiler Warning C5248

section 'section-name' is reserved for C++ dynamic initialization. Variables manually put into the section may be optimized out and their order relative to compiler generated dynamic initializers is unspecified

Remarks

The Microsoft C++ compiler uses reserved section names for internal implementation of features such as C++ dynamic initialization. If your code inserts a variable in a reserved section, such as .CRT$XCU, it interferes with the compiler. Your variable isn't considered a C++ dynamic initializer. Also, its relative initialization order compared to compiler generated dynamic initializers isn't specified.

To resolve this error, don't create a section that uses the reserved name or insert a variable in the reserved section.

There's no C++ standard conforming way to initialize variables across translation units, in a specific relative order with compiler generated dynamic initializers. Ways to force initialization before or after compiler generated C++ dynamic initializers are implementation-specific. For more information on Microsoft-specific implementation details, see CRT initialization.

Compiler Warning C5248 is new in Visual Studio 2019 version 16.11. It's off by default. For more information on how to enable this warning, see Compiler warnings that are off by default.

Example

Code that tries to emulate the C++ compiler behavior for dynamic initialization often takes this form:

void f();
typedef void (*type)();

#pragma section(".CRT$XCU", read)
__declspec(allocate(".CRT$XCU")) type i = f;

This code creates a section using a reserved name, .CRT$XCU. It stops the compiler from creating the section with the expected properties, and it may skip other initializations. The variable i placed in the section is a regular variable, and isn't considered an initializer by the compiler. The compiler may optimize i away. The relative order when f gets called compared to other dynamic initializers is unspecified.

If initialization order isn't important, you can use this pattern to dynamically initialize a variable at startup:

void f();

struct init_helper {
    init_helper() { f(); }
};

init_helper i;

See also

CRT initialization