In C, you'd often/sometimes (as a matter of style) use a file-scope
static variable where you'd use a private class member variable in C++. When scaling to multithreaded programs, simply adding
thread_local in C11 or the long-supported extension
__thread fits well. I know you can do exactly the same in C as C++ by putting everything inside a
struct and making a set of functions that takes a pointer to that
struct as its first argument. Some libraries do this extensively. But my personal style is to keep a
struct as small as possible, if needed.
I often read or hear some people arguing 'global' variables are so much bad. I follow their reasons, and most of their argument seems to be related to
extern global variables in C terms. What they say is certainly true. I sometimes use 1 or 2 of
extern declared variables throughout the whole program when it'll simplify things a lot and when it's easy to keep track of them, but going further will easily make a program unpredictable.
static variables? Do they still have the same problem as 'real' global variables? Maybe I don't even have to ask this question and go on if I think what I'm doing is right, but today I saw another 'global variables are BAD' kind of post, and finally came here thinking perhaps this is a right place for such kind of question. What is your thought?
This question is not a duplicate of this because this question asks about
static non-local variables while the other question is about file-scope and block-scope