1

I am trying to create bunch of void methods and call them later in program. I will demonstrate the code ahead to help better understand my issue.

.h file
static float sfloat;
namespace someNamespace
{
static void foo();
}
.cpp file
void someNamespace::foo(){cout<<sfloat<<endl}
  • above code is simpler version of class that I was working on.

I initialize sfloat in other .cpp file

otherFile.cpp
void initializeAndUseFoo(){sfloat = 5; someNamespace::foo();}

As far as my understanding goes, I expect foo to print out 5 but it prints out 0 instead. This behavior occurs across all other static variables that I have as well(pointer included). It seemed that somehow the variables inside the function are never initialized as the value I assign.

However if I call out "sfloat" not through the function then I can call it out properly.(if I just print it out on console with just

cout<<"just print it not through the function : " <<sfloat<<endl;

then it is indeed 5

Thank you for reading.

4
  • 5
    static when used on global variables creates a copy of that variable per TU. Commented Oct 18, 2012 at 7:23
  • could u please if you are kind enough explain what you mean by TU? Commented Oct 18, 2012 at 7:26
  • 1
    @user1217203 ...a copy per Translation Unit. So there will be duplicates. You need one static variable in one Translation Unit. Copy that statement into one .cpp file, and change the original to say extern instead of static. Then the compiler will know not to redefine the variable and the linker will know to look for the single translation unit with the static defined. Commented Oct 18, 2012 at 7:31
  • Thank u peter wood i ll try to use that approach :) Commented Oct 18, 2012 at 7:49

2 Answers 2

4

A global static variable is static within one compilation unit. If you make another compilation unit, it will have a separate copy of that static variable.

This SO question explains what a compilation unit is in C++.

Since your variable is static and global in the same time, there will be a single instance of it in it's compilation unit. If you want to access that exact variable from another file, you will have to use the extern keyword. Without it, a new copy of that variable will be created in each compilation unit. You can find some information here, for example.

Sign up to request clarification or add additional context in comments.

5 Comments

could u elaborate what you mean by "compilation unit" plus why would it be different from the first one? I mean it's static....? so I was expecting to have one variable named sfloat? It's not like I am creating a new one every time? or am I?
@user1217203 Yes, you're creating one everywhere the header is included.
@user1217203 Simply put, a .cpp file.
Thank u singer petrr and christian. I really appreicate all of u guys <3
I chose this as an answer because he not only provides a great insightful but also a direction to rich my understanding. I wish i could choose both as an answer though. Ty fellas
3

That's not a class, it's a namespace. static inside the namespace or at global scope gives methods and variables internal linkage. That means a copy of each will be available for each translation unit.

Because you modify (not initialize) sfloat in otherFile.cpp, only that version of the variable is modified. The original, initialized in .cpp file, retains the same value (which is the version printed by someNamespace::foo().

5 Comments

Thank you so much for explaining such an unexpected behavior. My laptop is about to die so I can't really detail how greatful I am. However I would appreciate so much if you could hint me how then not "modify" but initialize it with a value that I want to initialize it with( I need to initialize the variable with the value from other class ) or if namespace exhibits such a weird behavior should I be better off using class instead? like M
Math library? make it like static class?
@user1217203 well, actually, a "global" static (not member) would be initialized when declared.
@user1217203 Hard to say what is the best approach, since your example usage is kind of weird, anyway. There is (or should be) usually no need for global variables. And templates (which I'm pretty sure you don't need/want) should be the only reason to use a class with all static methods instead of a namespace. Also, SingerOfTheFall gives a simple way to make your global variable "unique" in his answer.
I like ur name christian rau hehehe. This free learning opportunity really delights me. I wish i was as able as like some other programmers so that i can share and lead as well .:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.