Muokkaa

Jaa


Compiler Warning C5247

section 'section-name' is reserved for C++ dynamic initialization. Manually creating the section will interfere with C++ dynamic initialization and may lead to undefined behavior

Remarks

The Microsoft C++ compiler uses reserved section names for internal implementation of features such as C++ dynamic initialization. If your code creates a section with the same name as a reserved section, such as .CRT$XCU, it interferes with the compiler. It may prevent other dynamic initialization and cause undefined behavior.

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

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 C5247 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