Initializing variables in header C++ - c++

EDIT: correct function names, and added #pragma once
This is a very strong simplification of my problem, but if I do this:
A.h
#pragma once
static int testNumber = 10;
void changeTestNumber();
A.cpp
#pragma once
#include "A.h"
void changeTestNumber()
{
testNumber = 15;
}
B.h
#pragma once
#include "A.h"
// some other stuff
B.cpp
#pragma once
#include "B.h"
// some other stuff
main.cpp
#pragma once
#include "B.h"
#include <iostream>
int main(){
changeTestNumber();
std::cout<<testNumber<<std::endl;
return 0;
}
Why am I not getting testNumber = 15 at the call out?
What really happens when I use a function that is included in a header of my included header?
If I remove the static in from of int testNumber, I will get some error about my testNumber being initialized twice.
So is my header compiled twice when I do this?
Thanks in advance!

Aside from the obvious incorrect naming (which I assume was simply a matter of hastily creating an analogous example and is not the actual issue in your code), you need to declare the variable as extern in your .h/.hpp file. You cannot have an extern variable that is also static since the (one of the) use(s) of static is to keep the variable contained to within a single .cpp file.
If you change:
static int testNumber = 10;
in your A.h file to:
extern int testNumber;
and then in your A.cpp file do something like:
#include "A.h"
int testNumber = 10;
Now go ahead and run:
int main() {
//changeNumber();
std::cout << testNumber << std::endl; // prints 10
changeTestNumber(); // changes to 15
std::cout << testNumber << std::endl; // prints 15
std::cin.ignore();
return 0;
}
Be sure to fix the function names!

Goodies and others are certainly correct, but let me put one more step ahead:
static makes the definition local to the translation unit. So defining a static global variable in the header will result in as many copies as the translation units it is included. Unless that's not specifically what you want that's not the way
extern tells the compiler that the global variable exist somewhere, but is not defined and must be searched at link phase. To the linker to succeed, you need to define it somewhere (typically a source file where it make more sense to exist)
Omitting both of them will result in a "multiple definition" linker error, where more than one source includes an header.
Now, the 2nd case has two limitation:
it forces you to have a compilable source to instantiate a global object even in the case you are providing a template library (or an "header only" library) making delivery more complex as required
It exposes to the so called global initialization fiasco: if you initialize a global object with values taken from other global objects defined elsewhere, since C++ doesn't grant about their order of construction (that ultimately belongs to the way the linker works), you may have trouble in proper initialization and destruction of global objects.
To avoid all this, consider that
A global defined function, if explicitly declared as inline can be linked more times and
Template functions as well as in-class defined member functions are in-lined by default
static local object are created only once, the first time they are encountered
You can define global values in headers by making them static local to functions: like in
inline int& global_val() //note the &
{ static int z = 0; return z; }
the only drawback is that you have always to place a () upon every access.
Since the local value is unique and instantiated upon a call, this will ensure that, if there are dependencies between globals (think to int z=0 as int z=something_else()) they will be created in the order they are needed and destroyed in reverse order, even in case of recursion and multiple threads (since c++14)
Considering the evolution of C++ towards generics and functional paradigms, and considering that placing all sources in a single compilation unit is sometime preferable than linking many sources... Have a think about not using global variables, but replacing them with inlined instatiator functions.
Editing about 2 years later:
C++17 have finally introduced the inline directive for also for variable declarations, just as a syntactic shortcut to the function expansion.
So -today- you can simply write
inline const float PI = 3.14159;
inline int goodies = 25;

A.h
extern int testNumber;
void changeNumber();
A.cpp
#include "A.h"
int testNumber = 10;
void changeTestNumber()
{
testNumber = 15;
}
B.h
#include "A.h"
// some other stuff
B.cpp
#include "B.h"
// some other stuff
main.cpp
#include "B.h"
#include <iostream>
int main(){
changeTestNumber();
std::cout<<testNumber<<std::endl;
return 0;
}
Please try like that.

Related

Correct way of initializing a class globally in C++

I know global is bad but just as a practice, is this the correct way to initialize a global class used between multiple object files?
Header 1.h
class test {
int id;
public:
test(int in){
id = in;
}
int getId(){
return id;
}
};
extern test t;
File 1.cc:
#include <iostream>
#include "1.h"
int main(){
std::cout << t.getId() << std::endl;
return 0;
}
File 2.cc:
#include "1.h"
test t(5);
Now what if instead of extern I use the static approach globally static test t(0); in the header?
Correct me if I'm wrong but that would compile fine however I would have 2 different unrelated copies of the same t in both object files and the final binary? Is that bad? Or does the linker sort it out to eliminate multiple copies?
There are global instances, not global classes.
What you have is a global instance. And yes, this sounds about right, until you get to multiple global instances which depend upon each other. Then the real fun will start.
Defining a variable as 'static' at global level means the variable will be defined in the compilation unit only (i.e. the '.o' file) and the symbol won't be exported by the compiler.
In other words: yes, there will be multiple variables with the same name but only visible to functions on the same compilation unit.
Besides, 'invisible' doesn't mean 'inaccessible'. You still can provide access to the variable. For example:
1.h
struct Test { int value; }; // Class definition
Test& get_t(); // Function declaration
1.cc
#include "1.h"
static Test t; // Variable declared as 'static'
Test& get_t() { return t; };
2.cc
#include "1.h"
#include <iostream>
int main()
{
std::cout << get_t().value << std::endl; // Static variable accessed
}
I use the static approach globally static test t;?
But your test class needs an int in parameter for the constructor, so you want:
static test t(0); // or whatever int you want
If you turn the extern to static in the header, you would define a static variable in each compilation unit in which the header is imported. So classes in different cpp files would no longer "communicate" via t, since each would have theirs. This is very error prone.
In addition, adding the definition of a static in a header is an extremely bad practice. When someone includes a header, one does not expect that it will create variables.
Including the declaration of t as extern is an acceptable practice. But be aware that if the header has a general purpose, this might reduce its reusability other projects.
More information of interest for you:
Must read: C++ Core Guidelines about source files
Must read: Guidelines for writing headers
StackOverflow: When are global variables recommended
Why global variables should be avoided if possible and when are they ok
If you put a variable declaration outside of any function, you're declaring the variable as 'global'. Ex:
1.cc
int this_is_global;
From here on you can use the variable in any function of '1.cc'.
For using the same variable in any other file, the compiler will need to know about it:
2.cc
extern int this_is_global;
Here, the keyword extern tells the compiler that the variable is declare somewhere else, letting the task of finding it to the linker.
If you miss to add the extern keyword here, the compiler will treat it as a new variable, and the linker will have two variables with the same name and will emit an error. All of your source files of your project except the first one will need the extern keyword to avoid duplicate symbols.
So common practice is to add the 'extern' declaration in an include file:
1.cc
int this_is_global;
1.h
extern int this_is_global;
2.cc
#include "1.h"
On the other side, the static keyword tells the compiler not to export the symbol. In other words: the variable will exists only in the source file it is declared. You could declare it once per source file and there will be different variables with the same name. Ex:
1.h
static int my_var;
1.cc
#include "1.h"
2.cc
#include "1.h"
This way, you'll end having two variables 'my_var' and changes to any of them won't affect the other.

static variable cpp do not want to change

FileA.hpp:
static int a;
void change(int);
FileA.cpp
#include "FileA.hpp"
void change(int x) { a = x; }
main.cpp
#include "FileA.hpp"
#include <cstdlib>
#include <iostream>
int main()
{
a = 5;
std::cout<<a<<std::endl;
change(10);
std::cout<<a<<std::endl;
a = 20;
std::cout<<a<<std::endl;
system("Pause");
return 0;
}
My output is:
5
5
20
Can someone help me with this? Why variable 'a' don't want to change in function
which is in FileA.cpp.
How to fix this. When I make change(int x) inline in "FileA.hpp" it works fine.
The static keyword on a global variable gives that variable internal linkage. It means that any translation unit that has that definition will have its own copy of the object. So the a object that main.cpp sees and that FileA.cpp sees are different objects. change will modify one of them, but main will output the other.
If you were intending static to mean that the object has static storage duration, global variables (or variables at namespace scope in general) have static storage duration anyway. You don't need to mark them static. However, if you remove static, you'll have another problem; you'll have multiple definitions of a across translation units.
The correct way to do this is to declare a as extern in the FileA.hpp file:
extern int a;
Then in a single translation unit (probably in FileA.cpp, define the object:
int a;
This means that any object that includes FileA.hpp will have the declaration of a (which is fine) and only one translation unit will have the definition of a. Perfect.
You declare a in the header file, which when included creates two copies of it, one in main.cpp and one in FileA.cpp. When you do a = 5; you assign to the copy from main.cpp, while when calling the function change, it changes the copy in FileA.cpp.

Not able to understand static behaviour

I wrote in
// In file t.h
#ifndef __t_h__
#define __t_h__
static int abc;
#endif
--
//In main.c
#include <stdio.h>
#include "t.h"
int main()
{
abc++;printf("%d \n", abc);
test();
}
- -
//In test.c
#include <stdio.h>
#include "t.h"
void test()
{
abc++;
printf("%d \n", abc);
}
When I run the project I found the output of abc is 1 and 1.
But when I change it to int abc in t.h. The output of abc = 1 and 2.
Why static is not retaining the value when the control reaches to test.c file.
If it won't retain then why should not it provide an error as the static variable can not be shared between/among the files?
static variables has internal linkage which means each translation unit gets its own copy.
So in your program each .cpp file which includes t.h has its own copy of the static variable, which in turn means, there are two objects in the memory. You can try printing their addresses to confirm this, as they will be different.
That makes the situation pretty simple: if you change the object in one .cpp, it doesn't reflect in the other .cpp file, because the object in the other .cpp file is a different object. Why should it change?
But when you change it to int abc (i.e don't make it static), then each translation unit has same object. If you change it in one file, it will be reflected in other file also, as expected.
As for sharing, then yes, static objects can be shared between two functions in the same translation unit, but they cannot be shared between two translation units.
Search for translation unit on this site, you will get many topics on it. Read them, then you will understand it fully.
When the preprocessor is done with your code, main.c looks like
// omitted stuff from stdio
static int abc;
int main()
{
abc++;printf("%d \n", abc);
test();
}
and test.c looks like
// omitted stuff from stdio
static int abc;
void test()
{
abc++;
printf("%d \n", abc);
}
So each file contains its own variable abc that is not accessible from the other.
One solution would be to change t.h to
#ifndef __t_h__
#define __t_h__
extern int abc;
#endif
and then change main.c to
#include <stdio.h>
#include "t.h"
int abc;
int main()
{
abc++;printf("%d \n", abc);
test();
}
You can think about it like this: there's now only one int abc in your program, in main.c but test.c knows about its existence because the extern int abc tells test.c that somewhere else in the project there is an integer called abc that it will be able to find at link time.
When you declare a static variable in header file a copy of the static variable is created in each Translation unit where the header is included. So each of the translation units involved in your program has its own copy of abc now and hence you get the observed behavior.The behavior is not what you expected but it well defined one.
static variable can not be shared between/among the files?
No they cannot be! that is the very purpose of making them static
static variables have internal linkage. Their scope is restricted to the the translation unit in which they are declared. They cannot be accessed beyond the TU.If you want to share the same variable across different Translation units You should drop the static and use extern, which gives the variable an external linkage and hence visibility across different translation units.
Good Read:
How do I use extern to share variables between source files?
In C, static has two usages:
1, Use static keyword to limit the var's scope in the translation unit. To make this simple, if you have two files: a.c, b.c and you wrote:
static int varA;
in a.c, then this means varA could only be used in a.c, if you want to use varA in b.c, you should remove the static keyword, and add extern int varA; in b.c, what people usually do is we create another file called: a.h, and write extern int varA; in a.h, and we simply include "a.h" in b.c, so we could write every variable we want to extern in a.h and use a single include "a.h" to make these variables or functions legal in other .c files(i.e. source files)
2, Using static to define a local variable in a function, for instance:
int TheFunction()
{
static int var = 0;
return ++var;
}
Because you used the static keyword on a local variable var, this variable will not loss when TheFunction() is returned.
The first time you call TheFunction() you will get 1, the second time you call TheFunction() you will get 2, and so on.
Next, lets see the usage of static in C++.
Because any C++ complier can complie a C code, so the 2 usages above is also in C++.
another two usages is:
1, static member variable.
2, static member function.
Lets see the code directly:
#include <iostream>
using namespace std;
class Test
{
public:
Test() : m_nNormalVar(0)
{
}
public:
// You need to init this static var outside the class
// using the scope operator:
// int Test::m_nStaticVar = 0;
static int m_nStaticVar;
// You can init this const static var in the class.
const static int m_nConstStaticVar = 10;
// This is just a normal member var
int m_nNormalVar;
};
int Test::m_nStaticVar = 0;
int main(int argc, char *argv[])
{
Test a;
Test b;
a.m_nStaticVar++;
a.m_nNormalVar++;
cout << b.m_nStaticVar << endl;
cout << b.m_nNormalVar << endl;
return 0;
}
a and b are objects of class Test they have the same m_nStaticVar and the same m_nConstStaticVar, but they have their own m_nNormalVar this is a static member variable.
#include <iostream>
using namespace std;
class Utility
{
public:
// This is a static member function, you don't
// need to have a concrete object of this class
// to call this function.
static int SelectMax(int a, int b)
{
return a > b ? a : b;
}
};
int main(int argc, char *argv[])
{
// No objects of class Utility
cout << Utility::SelectMax(2, 1) << endl;
return 0;
}
So this is a static member function of a class in C++.
These four ways of static's usage is all I known, if there are still some other usages, please help to edit this post, thx:)
EDIT:
Add static global function
1, Use static keyword to limit the function's scope in the translation unit. To make this simple, if you have two files: a.c, b.c and you wrote:
static void StaticFunction();
in a.c, so you can only call StaticFunction() in a.c, if you want to call this function in b.c you should remove the static keyword and then delcare it before the usage. Or just declare it in a.h and include "a.h" in b.c

When to use extern in C++

I'm reading "Think in C++" and it just introduced the extern declaration. For example:
extern int x;
extern float y;
I think I understand the meaning (declaration without definition), but I wonder when it proves useful.
Can someone provide an example?
This comes in useful when you have global variables. You declare the existence of global variables in a header, so that each source file that includes the header knows about it, but you only need to “define” it once in one of your source files.
To clarify, using extern int x; tells the compiler that an object of type int called x exists somewhere. It's not the compilers job to know where it exists, it just needs to know the type and name so it knows how to use it. Once all of the source files have been compiled, the linker will resolve all of the references of x to the one definition that it finds in one of the compiled source files. For it to work, the definition of the x variable needs to have what's called “external linkage”, which basically means that it needs to be declared outside of a function (at what's usually called “the file scope”) and without the static keyword.
header:
#ifndef HEADER_H
#define HEADER_H
// any source file that includes this will be able to use "global_x"
extern int global_x;
void print_global_x();
#endif
source 1:
#include "header.h"
// since global_x still needs to be defined somewhere,
// we define it (for example) in this source file
int global_x;
int main()
{
//set global_x here:
global_x = 5;
print_global_x();
}
source 2:
#include <iostream>
#include "header.h"
void print_global_x()
{
//print global_x here:
std::cout << global_x << std::endl;
}
It is useful when you share a variable between a few modules. You define it in one module, and use extern in the others.
For example:
in file1.cpp:
int global_int = 1;
in file2.cpp:
extern int global_int;
//in some function
cout << "global_int = " << global_int;
It's all about the linkage.
The previous answers provided good explanations about extern.
But I want to add an important point.
You ask about extern in C++, not in C and I don't know why there is no answer mentioning the case when extern comes with const in C++.
In C++, a const variable has internal linkage by default (not like C).
So this scenario will lead to linking error:
Source 1 :
const int global = 255; //wrong way to make a definition of global const variable in C++
Source 2 :
extern const int global; //declaration
It needs to be like this:
Source 1 :
extern const int global = 255; //a definition of global const variable in C++
Source 2 :
extern const int global; //declaration
This is useful when you want to have a global variable. You define the global variables in some source file, and declare them extern in a header file so that any file that includes that header file will then see the same global variable.

static char array behaving unexpectedly across classes

I know this will have a easy answer but am stumped. I've simplified the code as much as possible to get to the point.
I have a simple header file, call it a.h
which has the below and nothing more (it acts as a global settings file for all files in my project that choose to include it)
#ifndef A_H
#define A_H
namespace settings{
static char name[16]={'\0'};
}
#endif
I then have another class, with its own header file, lets call it b.cpp (with b.h not shown)
#include "a.h"
void B::doSomething()
{
strcpy(settings::name,"I like Dogs");
}
Lastly, the third class which access' settings::name, call it c.cpp (with c.h not shown)
#include "a.h"
void C::printSomething()
{
printf("Some Girls Say %s\n",settings::name);
}
Alas, all that gets printed is "Some Girls Say ". What gives? I don't understand how settings::name isn't surviving function destruction of B::doSomething() (I can only guess this is the issue). Is strcpy being lazy and just pointing settings::name to where "I Like Dogs" begins rather than actually acting like strdup?
Any help and workaround is much appreciated. Thanks!
EDIT: For further clarity, B.doSomething() is invoked before C.printSomething().
the static keyword links the name into each translation unit it is included in, so each .cpp file basically has its own version of name.
What you want to do is put the name you want to share in one .cpp file (without the static and declare it with extern linkage in the .h file.
so that's:
a.cpp
namespace settings {
char name[16] = { 0 };
}
a.h
namespace settings {
extern char name[16];
}
b.cpp
void B::doSomething()
{
strcpy(settings::name,"I like Dogs");
}
c.cpp
#include "a.h"
void C::printSomething()
{
printf("Some Girls Say %s\n",settings::name);
}
By including that header in two source files, you have two separate storage locations. Including the header is a bit like just pasting that code into the source files. So they each have their own static variable called 'name'.
To get this to do what you want, you need to:
Use extern, not static, in the header. This effectively means each file including the header will expect to reference a variable external to it.
Define the variable in one of your source files. It needs to be defined somewhere, once. Don't use static or extern when defining it.
First of all write in a.cpp
namespace settings{
char name[16];
}
then ensure
B::doSomething()
is called.