Link to home
Start Free TrialLog in
Avatar of forums_mp
forums_mp

asked on

Global data

I've seen a handful of source where there needs to be global data defined for one reason or another. Usually in the C-language this is accomplished by creating a header file called globals.h where extern variables are declared and a file called globals.c where the variables
are defined.

Now with regards to C++, given:
[code]
// global_file.h
#ifndef GLOBAL_FILE
#define GLOBAL_FILE
  static int const myGlobal1 = 0xCAFE ;
#endif

///foo.h
class foo {
  public:
    foo () {}
    void whatever();
};
//foo.cpp
# include "global_file.h"
foo::foo()
{}
void foo::whatever()
{
  //do something with myGlobal1
}

Similarily:

///bar.h
class bar {
  public:
    bar () {}
    void whatever();
};
//bar.cpp
# include "global_file.h"
bar::bar()
{}
void bar::whatever()
{
  //do something with myGlobal1
}
[/code]

The question(s)
a) If memory serves the static qualifier in global_file is 'irrelevant'.  True/False?
b) Does each translation unit (by that i mean foo and bar) get their own copy of myGlobal1?
c) What's a viable solution to this issue.   i.e how do I ascertain there's only _one_ copy and only _one_ copy at all times for each translation unit?  one thought I have surrounds the use of a class with public members.
    class global_data {
    public :
       static int const myGlobal1 = 0xCAFE ;
     };
d) Is there a namespace solution for this (don't think wrapping the constants in a namespace will resolve anything but ..just checking)? for instance do I create a class and make every element public members within the class or ... )
Avatar of Gregdo
Gregdo
Flag of Canada image

a) Yes, static makes no difference on global variable.
b) Since you create the variable in the header file every c or cpp file that includes that header file will get it's own global variable.
c) see below
d) no
// global_file.h
#ifndef GLOBAL_FILE
#define GLOBAL_FILE
  extern int const myGlobal1 ;
#endif
 
// global_file.cpp
int const myGlobal1 = 0xCAFE ;
 
// The rest of your code remains the same

Open in new window

Avatar of forums_mp
forums_mp

ASKER

|| b) Since you create the variable in the header file every c or cp
|| p file that includes that header file will get it's own global variable.
Which means these translation unit will increase by '4' bytes (assuming sizeof int = 4 )

Does the same rule apply for enumerated types - assuming the enum type XYZ was defined in globals.h?

  enum XYZ {
  ABC  = 5,
  CDE  = 6
};
ASKER CERTIFIED SOLUTION
Avatar of Gregdo
Gregdo
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
|| If you use "static" then the variables will be global only within the
|| scope of the "translation unit" that you use them in.
Right, however, I thought this statement also holds true without the use of 'static'?
SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So I'm working through the scenarios that gregdo and yourself (evilrix) propose.  Extern certainly doesn't work  for arrays.. so I'm thinking about the enum option.   Now instead of doing enum XYZ { variable_c = 15, }; I was able to do enum { variable_c = 15, }; Not sure if i understand the difference?
#ifndef HEADER_H
#define HEADER_H
 
static int const variable_a = 15; // [1]- what needs to be fixed
extern int const variable_b ;     // alternative - wont work for arrays
extern double const PI ; 
 
enum  { 
  variable_c = 15 ,
};
 
// alternative
class bar 
{
public :
  static int const variable_d = 15 ;
};
 
#endif
 
//header.cpp
 
 
# include "header.h"
 
extern int const variable_b = 15 ;
extern double const PI = 3.141 ;
 
 
//main.cpp
# include <iostream>
# include "header.h"
 
int a_array [ variable_a ] ;
int b_array [ variable_c ] ;
 
int main() 
{
 
  std::cout << bar::variable_d << std::endl;
  std::cin.get(); 
}

Open in new window

the first allows you to define types of XYZ. the second is an anonymous enum so you can't define any type from it.
does an anonymous enum follow the same 'rules' as one that's not anonymous?
|| b) AS long as you define the global to be static each translation gets its own copy
|| (because it has no external linkage) but f you remove the static keyword you will
||  get linker errors because then it will have external linkage and break the one definition rule
Are you saying that if I dropped the keyword off static i.e  
    int const variable_a = 15; // [1]- what needs to be fixed

I'll get linker errors if multiple translation unit include header.h?
Anonymous and named enumerated types are semantically the same with the exception that you can't create an instance of an anonymous one.

The reason you don't get a link error when using the static keyword is because the type has no external linkage. This means the types name is not exported from the translation unit so each has its own unique instance. Remote the static keyword and the type will have linkage. Since a symbol for am instance of an instanciated type can only exist once, due to the one definition rule, you will get a link error. This is because each translation unit that includes the header instantiates a type with the same symbol that has linkage.