MISRA Discussion Forums

Full Version: Proposal: Consider adding a rule for the undefined behaviour in ISO/IEC 14882:2003 3.6.2 section 3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

as far as i know, there is no explicit rule to deal with that.
12-8-1 comes close but is for copy constructors only.

An implementation is permitted to perform the initialization of an object of namespace scope with static
storage duration as a static initialization even if such initialization is not required to be done statically, provided
that
— the dynamic version of the initialization does not change the value of any other object of namespace
scope with static storage duration prior to its initialization, and
— the static version of the initialization produces the same value in the initialized object as would be produced
by the dynamic initialization if all objects not required to be initialized statically were initialized
dynamically.
[Note: as a consequence, if the initialization of an object obj1 refers to an object obj2 of namespace
scope with static storage duration potentially requiring dynamic initialization and defined later in the same
translation unit, it is unspecified whether the value of obj2 used will be the value of the fully initialized
obj2 (because obj2 was statically initialized) or will be the value of obj2 merely zero-initialized. For
example,
44
ï›™ ISO/IEC ISO/IEC 14882:2003(E)
3 Basic concepts 3.6.2 Initialization of non-local objects
inline double fd() { return 1.0; }
extern double d1;
double d2 = d1; // unspecified:
// may be statically initialized to 0.0 or
// dynamically initialized to 1.0
double d1 = fd(); // may be initialized statically to 1.0
—end note]
3 It is implementation-defined whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an object of
namespace scope is done before the first statement of main. If the initialization is deferred to some point
in time after the first statement of main, it shall occur before the first use of any function or object defined
in the same translation unit as the object to be initialized.31) [Example:
// – File 1 –
#include "a.h"
#include "b.h"
B b;
A::A(){
b.Use();
}
// – File 2 –
#include "a.h"
A a;
// – File 3 –
#include "a.h"
#include "b.h"
extern A a;
extern B b;
int main() {
a.Use();
b.Use();
}
It is implementation-defined whether either a or b is initialized before main is entered or whether the
initializations are delayed until a is first used in main. In particular, if a is initialized before main is
entered, it is not guaranteed that b will be initialized before it is used by the initialization of a, that is,
before A::A is called. If, however, a is initialized at some point after the first statement of main, b will
be initialized prior to its use in A::A. ]
The example is a 0-3-1 & 8-5-1 violation, but we will consider an explicit rule for the next version