Tuesday, March 23, 2010

Avoid Directly Access Globals




Do not directly access global data from a constructor (AvoidDirectlyAccessGlobals.rule)


Description:

Directly accessing global data from a constructor is risky because the global object may not yet exist when the "other" static object is initialized. This rule detects if you directly access global data from a constructor.

Benefits:

The order of initialization of static objects defined in different compilation units is not defined in the C++ language definition. Therefore, accessing global data from a constructor may result in reading from uninitialized objects.

Example:

int a;

class A
{
public:
    A();

private:
    int b;
};

A::A()                  // Violation
{
    b = a;
}



Repair:

int a;

class A
{

public:
    A();

private:
    int b;
};



A::A()
{ // OK
    // Do not access global value 'a' in constructor
}

Author
ParaSoft


More: http://jmmv.livejournal.com/45984.html



No comments:

Post a Comment

Labels