Home > Software design >  How should I initialize my global variables?
How should I initialize my global variables?

Time:08-27

I have a bunch of global variables I like to initialize with specific functions, something like that:

// MyFile.h

typedef struct myValues
{
    int a;
    int b;
    int c;
} myValuesType;
 
extern myValuesType values[5];
 
void Initialize(void);
 
 
// MyFile.c

myValuesType values[5];
 
// This gets called in main() before everything else
void Initialize(void)
{
    values[0].a = 1;
    values[0].b = 2;
    values[0].c = 3;
 
    // ETC
} 

However I came across some code that did the initialization directly in the definition, like that:

// MyFile.h
typedef struct myValues
{
    int a;
    int b;
    int c;
} myValuesType;
 
extern myValuesType values[];
 
// MyFile.c
myValuesType values[] =
{
    {0,1,2},
    // Etc.
}

To me, the second implementation is much slimmer and maybe more clean for this particular example, but what I'm most interested in is actually happening: what is the difference between the two?

Am I making the computer do more stuff by defining them without any values, and then calling a function to initialize them?

Is it like in the second case it's the compiler that's doing the job and, on a large scale, this results in a much optimized code?

And lastly, the second implementation isn't always applicable, right? Like if I need to initialize them to values returned by functions, or if I should need to malloc the values, (or any other cases that come to your mind) I would NEED to create a function to initialize them, right?

But in the case of the snippets of code I posted, what is the better way to handle that?

CodePudding user response:

Transferring my comment into an answer.

Your first code is not "initializing" the variables — they are initialized to all zeros (because there is no explicit initializer), and your code assigns other values to them after the program is running.

Use the initializers as in the second example whenever possible — it is simpler and more efficient. When you use initialization, the system doesn't load the data with all bytes zero and then overwrite the memory in the function call. Also, there's no way to avoid getting the memory initialized properly, whereas with a function call, you might forget to call it, leaving the memory as "all bytes zero".

However, if you also need to reset the global variables to a known, "not all bytes zero" state after doing some calculations, then the function may well be the better choice — but you might also need to ask yourself "why do I have global variables that need such (re)initialization?"

If the initialization must use function calls or if dynamic memory needs to be allocated, then you must use an initialization function.

CodePudding user response:

Am I making the computer do more stuff by defining them without any values, and then calling a function to initialize them?

Yes. The compiler will create a function in the .exe file that writes the values into the variable (because that's what you told the compiler to do), and the computer will run it when the program runs.

To clarify: The compiler will create the Initialize function in the exe file. One commenter thought I meant the compiler would create an additional function of its own accord.

Is it like in the second case it's the compiler who's doing the job and, on a large scale, this results in a much optimized code?

Yes. The compiler will just write the values directly into the .exe file, in the data section for this variable. After the operating system loads the .exe file, the values will already be there and the computer won't have to do anything extra. Also, the values are directly in their final layout, whereas with the first choice, the function basically consists of a long series of "put this value at this address" meaning the addresses also have to be processed.

And lastly, the second implementation isn't always applicable, right? Like if I need to initialize them to values returned by functions, or if I should need to malloc the values, (or any other cases that comes to your mind) I would NEED to create a function to initialize them, right?

Yes, that's correct. The second is only applicable if you know all the values at compile time.


In C (which you are not using), you can always use the second one, though the compiler just converts it to the first one when it doesn't work. The compiler will insert function calls before main if needed, and it may also sometimes run the functions at compile time if they are declared as constexpr.

  • Related