Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What purpose does this code change serve?

Tags:

c++

I am trying to understand the implications / side effects / advantages of a recent code change someone made. The change is as follows:

Original

static List<type1> Data;

Modified

static List<type1> & getData (void)
{
    static List<type1> * iList = new List<type1>;
    return * iList;
}
#define Data getData()

What purpose could the change serve?

like image 597
Lazer Avatar asked Jan 14 '12 20:01

Lazer


1 Answers

The benefit to the revision that I can see is an issue of 'initialization time'.

The old code triggered an initialization before main() is called.

The new code does not trigger initialization until getData() is called for the first time; if the function is never called, you never pay to initialize a variable you didn't use. The (minor) downside is that there is an initialization check in the generated code each time the function is used, and there is a function call every time you need to access the list of data.

like image 150
Jonathan Leffler Avatar answered Oct 21 '22 16:10

Jonathan Leffler