0

After reading "ruhalde" comment on Jul 28 '11 at 11:56, extern keyword usage

I would like some advice on how to organize many variables, using EXTERN vs STATIC.

In that article, 1) definer and initializer are in a CPP file. 2) declaration with "extern", but no definition, in a separate .h file.

Say I have 100 global variables (but related somehow) in MULTIPLE CPP files, grouping them all in a single .h file is a good way to share them.

But this requires the developer to maintain (these related) variables in MULTIPLE CPP(s) and a header file. I have seen other developer use "static" to group all variables in a single .h file.

e.g. myheader.h

static int var1 = 1;
static int var2 = 2;
...

This is easier to maintain. But as far as I understand, these variables are no longer "global". The "static" keyword reduces the scope to the cpp file that includes this header file.

i.e.

foo1.cpp

#include "myheader.h"
void foo1()
{
  var1 +=1;
  var2 +=2;
  printf(....., var1, var2);
}

foo2.cpp

#include "myheader.h"
void foo2()
{
  var1 +=100;
  var2 +=200;
  printf(....., var1, var2);
}

var1, var2 in foo1 are DIFFERENT variables from var1, var2 in foo2. More seriously, every variable in myheader.h is recreated for every function that includes it.

Question: (assuming I am using "extern" and "static" correctly)

By not using "extern" to create truly global variables, is using "static" for easier code maintenance an acceptable alternative?

Community
  • 1
  • 1

3 Answers3

1

Say I have 100 global variables (but related somehow) in MULTIPLE CPP files,

is a very poor design! Simply don't do it!

By not using "extern" to create truly global variables, is using "static" for easier code maintenance an acceptable alternative?

It is simply a total different thing! As you wrote, static gives you every time you use it in a cpp file a new instance of it. There is nothing global with static!

I would advice you to

  1. Don't use global variables
  2. If you use global variables, place them in a hand full classes to make them easier to handle
  3. Remember that the initialisation of the vars/class instances will be depend of linking order. Never rely on that order!
  4. Take a look for singleton pattern
Klaus
  • 24,205
  • 7
  • 58
  • 113
0

You can put all variables in header, declaring them as extern. Now in a cpp define them. Then you can include the header into source files in which you would need these variables.

Arun
  • 3,138
  • 4
  • 30
  • 41
0

I would put static variables in a .h file only if they are constant. That way, every file that includes the .h file will get the same, unchanged, value.

As you have explained, it will be very confusing to users (and developers while debugging) to see the variables have different values in different functions.

For variables that are not const, I would advise declaring them extern variables. They can be defined in the appropriate .cpp file or files.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Thank you @r-sahu for this answer. " I would put static variables in a .h file only if they are constant. That way, every file that includes the .h file will get the same, unchanged, value." I had overlooked the "const" in the declaration. And adding "static" in front of the "const" reduces namespace pollution. – user3889961 Jul 30 '14 at 06:53
  • @user3889961, you are welcome. Glad I was able to help. – R Sahu Jul 30 '14 at 15:47