Find the answer to your Linux question:
Results 1 to 6 of 6
We all know that global variables give you cancer! But it's quite difficult to avoid them in a gtk program because gtk allows only one user-supplied argument to be passed ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,190

    Global variables in C programs


    We all know that global variables give you cancer! But it's quite difficult to avoid them in a gtk program because gtk allows only one user-supplied argument to be passed to an event handler. And handlers sometimes form chains: a handler opens a dialog window that contains widgets with their own handlers. Getting the necessary information to the handlers becomes a nightmare, so you just make the variable global so you don't have to pass it.

    I have a program with more global variables than I should like. And it just occurred to me that if I made them all fields of one big structure and declared that in main (so that it would be permanently on the stack), I could pass a pointer to it to every gtk event handler and the handlers could simply extract the particular information they needed.

    Technically that wouldn't be a global variable, but surely it would be the same thing by a different name. So why would that be all right when global variables are all wrong?
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,397
    Great question Hazel! Basically, it is information hiding, so you don't have conflicts with other code that may link with into your program. You can also make the structure an external static structure (or class) in the translation unit (source file) that contains main() to do the same thing. In C++ programs, it is not uncommon to encapsulate these sort of functions or data members in a class as static functions and class variables. Another method is to have a singleton class with these data elements as private member variables, and appropriate getter/setter methods. That provides a lot of opportunity to serialize access in multi-threaded applications where more than one thread may need access to these elements.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,190
    Firstly, I am using C, not C++. I don't really understand C++ and I have never used it. So no classes or objects please!

    What is an external static structure? I'm familiar with static variables inside functions. I also know that the word "static" can be used in quite a different way to make a function unique to the file it's declared in. But I can't quite make out what you're recommending.
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,397
    Quote Originally Posted by hazel View Post
    Firstly, I am using C, not C++. I don't really understand C++ and I have never used it. So no classes or objects please!

    What is an external static structure? I'm familiar with static variables inside functions. I also know that the word "static" can be used in quite a different way to make a function unique to the file it's declared in. But I can't quite make out what you're recommending.
    Ok. I'll stick with C except for this, that C++ classes are the same as C structures, but with code embedded in them that can operate on the data members. By a static structure, I mean one that is declared static in a translation unit (source file). It is nominally only visible in that source file, although pointers to it, or its members, can be passed to other functions that are not defined in the same source file. That way, they are not global, and can only made "visible" by functions in that source file. Clearer?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,190
    Oh, I thought you could only do that with functions; I didn't know it works for variables too. But the variables that I am talking about are needed in more than one module so, if they are not declared as global, they will ultimately have to passed as some kind of arguments. In which case, they might as well be declared inside the main function, if I can get them all to be accessible via a single pointer that can be passed around.

    So basically you're saying that that's an OK way to solve the problem.
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,397
    Yes. The producer has to be able to control access to the data. It can pass a non-const pointer to functions it trusts to safely modify the data, or a const pointer to those who only need to read the data. Of course, the receiver of the pointer can cast away const-ness, but that is another issue. The important thing is making your intentions clear. IE, you (the receiving function) can access my data, but I am showing my intentions on how you may use it. If the variable is global, then you are leaving your door unlocked and a sign outside that states "come in and take what you want", so to speak.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •