|ARM Technical Support Knowledge Articles|
The way in which C and C++ handle multiple definitions of the same global variable is very different to each other.
The C standard1 implements a concept named tentative definitions according to which:
"A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier
static, constitutes a tentative definition".
Multiple declarations of the same global variable are treated as tentative definitions and integrated into a single definition at the end of the C compilation process.
int bar; // Tentative definition 1
int bar; // Tentative definition 2
int fn_dummy(int a, int b)
int bar=5; // Definition
int bar; // Tentative definition 3
int fn_dummy2(int a)
Here the three declarations of
bar are considered to be tentative definitions. These are integrated into the one actual definition, which has a value of 5. If there is no actual definition then the compiler initializes the value of the global variable to zero.
The C++ standard2 however, does not allow tentative definitions and multiple global variable declarations are therefore reported as errors by the C++ compiler.
1 [Section 6.9.2 - C Standard Specification ISO/IEC 9899:TC2]
2 [Annex C.1.2, Clause 3.1 - C++ International Standard ISO/IEC 14882-1998]
Article last edited on: 2015-04-23 15:12:34
Did you find this article helpful? Yes No
How can we improve this article?