Main Content

MISRA C++:2023 Rule 6.7.2

Global variables shall not be used

Since R2024b

Description

Rule Definition

Global variables shall not be used.

Rationale

A global variable is:

  • A variable declared in namespace scope, including the global namespace.

  • A static data member in a class.

Depending on the linkage of such a variable, it can be read or written from anywhere in a source file or a program. Such widespread access to nonlocal objects can lead to various problems:

  • Because many functions and entities can read or write these global variables, their interactions become unpredictable.

  • Because multiple functions can access these global variables simultaneously, concurrent programs can face the risk of undefined behavior caused by a data race.

  • Because the order of initialization of these global variables is not completely specified by the C++ standard, the value of such a variable can be unpredictable if the variable is initialized dynamically.

Avoid using global variables, except for these variables that do not violate the rule:

  • constexpr variables

  • const variables that are initialized through static initialization

  • Variables holding a lambda without lambda capture

Polyspace Implementation

Polyspace® reports a violation of this rule if you declare any of these objects:

  • A global or namespace-scope variable that is neither const nor constexpr

  • A dynamically initialized const global or namespace-scope variable

  • A static class member

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

This example shows Polyspace reporting violations of this rule on:

  • Non-const variables declared in the global namespace, such as nonConstGlobal and threadLocalGlobal

  • const global variables that are initialized using dynamic initialization, such as constGlobalDynamicAlloc

  • Namespace-scope variables, such as namespaceScopeVar

  • Static class member, such as myClass::staticClassMember

int getVal();
constexpr int getConstexprVal();

int nonConstGlobal; //Noncompliant - variable declared in global namespace scope
thread_local unsigned int threadLocalGlobal= 1; //Noncompliant - global variable that is local to a thread
int constexpr constantExpressionGlobal {0}; //Compliant by exception
int const constGlobal{0}; //Compliant by exception
int const constGlobalDynamicAlloc{getVal()}; //Noncompliant

namespace{
int namespaceScopeVar; //Noncompliant - variable declared in namespace scope
int constexpr namespaceScopeConstexprVar{getConstexprVal()}; //Compliant by exception
}

class myClass{
static int staticClassMember; //Noncompliant - static data mmeber
};

auto globalLambdaNoCapture = [](){/**/} //Compliant by exception

Polyspace does not report violations for these exceptions to the rule:

  • constexpr variables such as constantExpressionGlobal or namespaceScopeConstexprVar

  • Statically initialized const variables, such as constGlobal

  • Variables that hold a lambda without lambda capture, such as globalLambdaNoCapture

Check Information

Group: Basic Concepts
Category: Required

Version History

Introduced in R2024b