Should I initialize a variable in a function or globally?

Viewed 1156

I'm writing my own game engine in C++ and I was wondering where should I initialize variables which I need every frame, for example, every time mouse pointer is getting moved, the coordinates of pointer position should be saved in a variable.
Should I initialize the variable for that globally or in the function where it always initializes the variable new? Where are the benefits and where are the disadvantages?

float offsetX
float offsetY
void mouse_callback(double posX, double posY) {
    offsetX = posX - lastX;
    offsetY = lastY - posY;
    lastX = posX;
    lastY = posY;
}

or

void mouse_callback(double posX, double posY) {
    float offsetX = posX - lastX;
    float offsetY = lastY - posY;
    lastX = posX;
    lastY = posY;
}
5 Answers

Variables should be defined as closely as possible to where they are used. The longer the distance between declaration and initialization, the more error prone the code will be.

A good thing about having the variables defined in the function, is that you can mark them as const(!). Then you notify the readers of the code, that these variables will never change.

Remember that you write code for people and not for the compiler. For this simple use case, there will probably be no difference in memory nor speed - the compiler will be able to optimize away the variables.

Should I initialize the variable for that globally or in the function where it always initializes the variable new?

I think you're confusing initialization with declaration. Looking at your code:

void mouse_callback(double posX, double posY) {
    float offsetX = posX - lastX;
    float offsetY = lastY - posY;
    lastX = posX;
    lastY = posY;
}

Here, you're declaring offsetX and offsetY to be local variables, and initializing them to posX - lastX etc. But then you don't do anything further with those variables, and the values you calculated will disappear as soon as mouse_callback() exits. If you're actually using offsetX and offsetY inside mouse_callback() in your actual code and you don't need them anywhere else, that's probably fine. But as written above, you could just delete the first two lines in the body of the function and the function would still behave exactly the same.

lastX and lastY are clearly meant to be variables with longer lifetimes (i.e. global variables or perhaps instance variables in a class) because you use them in mouse_callback() without either declaring them or initializing them. Those clearly need to have valid values before mouse_callback() is called, because you're using them in the first two lines.

In general, the main thing that you need to know is that all variables need to be initialized, i.e. given valid values, before you use their values. Except for objects where initialization might be slow or resource-intensive, there isn't much reason not to initialize variables when you declare them, whether they're global or local, and doing so will help you avoid bugs that can be hard to track down.

It fully depends on your code.
If you need those variables just in one function (or class), initialize them inside it. But if you need it on multiple functions (globally), you may initialize them globally.
Using less global variables (usually) makes your code more memory efficient, faster and safer. Search Google (or Bing like me!) for "global variables" for more information.

This is a question about optimization versus readability. Having the variables as class members is slightly faster. But readability dictates to create variables only were they are needed. So long as you do not need the slight improvement in speed, keep them in the function.

Local variables are shown in all debuggers (global are not) and are faster than global variables, but there is less available memory for local variables and you can use them in different functions. Choose what's better for you.

Related