Thread: Large number of variables needing global scope

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    596

    Large number of variables needing global scope

    I have a lot of globals in my program. Much of the program operation is related
    to setting and modifying various parameters, switches, flags, etc.
    Also changing operating modes, adding and removing indicators; in other words,
    all kinds of stuff.

    The program uses functions as subroutines to carry out the various operations.
    They don't return values as many library functions do, they just perform some
    operation. Subroutines.

    I could put everything in a big structure, and pass the structure address around,
    but that would basically give almost all the functions access to them. Which is
    one of the complaints about globals.

    One main part of the program's function is to generate fractal based images, and
    allow magnification of any selected part. The other main part provides setting up
    and editing a color spectrum which is used to color the image. Most operations are
    activated by mouse and keboard; some of the new ones will be in menus.
    It's not a complicated program, a main command interpreter and mouse interpreter
    call all the appropriate functions. But their operations affect many of the (global)
    variables.

    Does the number of variables in question have anything to do with whether or not
    globals are appropriate? In other words if one particular global variable was
    appropriate, would a large number of such variables also be appropriate?

    And also, what is the best way to deal with large numbers of variables that require
    such large scope?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by megafiddle View Post
    They don't return values as many library functions do, they just perform some
    operation. Subroutines.

    I could put everything in a big structure, and pass the structure address around,
    but that would basically give almost all the functions access to them. Which is
    one of the complaints about globals.
    I'm not sure why you think every function needs access to every value. Group the ones that are needed in every function in a structure, and the ones that aren't, pass appropriately. The structure is probably the way I'd go. You'd need to give me an example for me to really advise anything else.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    I didn't mean that every subroutine needed every variable. I meant that if all the variables were
    in a single structure, they would be accessable by any function, just as globals would be.

    That's an idea though. Put the common ones in a structure and add in the specific ones as needed.

    And I guess you were just saying that they don't need to all be in one structure.
    That would have just been a lazy way of doing it, putting them all in one structure.

    What about the number of global variables? Does the number of them matter?
    Are a few ok if appropriate, but not a large number?
    Last edited by megafiddle; 09-07-2011 at 08:38 PM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    General best strategy is to keep the number of global variables to an absolute minimum. Whenever possible pass stuff around between functions rather than have it all hanging out in the open... The shorter the lifetime of a variable the less chance of something screwing it up.

    As a rule I will put settings into a struct, as bit flags when possible, at global scope. I may globalize the main window handle and perhaps the instance handle, in window GUI programs. The rest will be either passed around between functions or exist at page scope in a single file.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    I have been trying to minimize the globals. It's actually not easy because it has always been
    my programming style to use them. I don't have to conform to an employer's standard or an
    acedemic standard. Even the software that I did write for an employer was for in-house use
    only, and I was free to use whatever necessary to get it done.

    I can understand that there a good reason for rules like "don't use globals". But I also noticed
    that the common programming style is maybe different from what I use. It seems like globals
    are rarely needed, when looking at typical examples of programs. It also seems though, that
    my programs aren't quite structered like those either.

    I am getting a sense that I am not structuring the program right and that is why I need to use
    globals. I will post my program as soon as I have cleaned up all the extraneous stuff. Much of
    it is experimental, as I am trying out various color rendering tools. But it is settling down and I
    can soon post the source code.

    But for the present, I was just wondering if the number of globals mattered; if you simply need
    a whole bunch of them, is that Ok?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Technically speaking, you are probably fine, most of the time. I skimmed the standard looking for something that identifies the number of objects you are able to have - such as they are known to do with explaining how many case statements you can have and such - but I didn't see one. It's possible the language just doesn't care how many variables you declare. Someone else here probably has an idea or could point you in the right direction if you don't feel like goggling for it.

    I tend to limit the scope of my variables as much as possible, only showing them to the minimum amount of functions that need to see them, so I've never really paid attention to any potential limit on the number of globals you can have.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    But for the present, I was just wondering if the number of globals mattered; if you simply need
    a whole bunch of them, is that Ok?
    Nope, it's not. I get the sense that you know what all this data is for, so why don't you focus on separating it. For example:
    One main part of the program's function is to generate fractal based images, and
    allow magnification of any selected part. The other main part provides setting up
    and editing a color spectrum which is used to color the image.
    These are very separate jobs, and the 64,000 dollar question (worth your (possible) wages) is do you really work with the same data when you are coloring the image as when you are magnifying? Can the image itself be a separate thing? Once you create types, you'll find it's easier to break up a problem. That's an OOPism, but you can easily do the inverse. What steps needs to happen to accomplish a goal and what data do you need for every solitary step? That can also help you break up the problem into tiny problems, and organize data.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Following upon whiteflags, I also extend that to what source pages things are on... For example, again in windows Gui mode... treeview controls bring a lot of overhead to a program so I will put all the functions related to a treeview on one source page... same with listviews, dialogs, and such. Plus I never put code in switch statements ( a cause of considerable disorganization in windows). What I do instead is use the switch statement to call functions, which encapsulates the code and makes debugging a lot simpler.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There's never really a good reason to have a large number of global variables. What constitutes a good reason or a large number is highly subjective of course.
    Unfortunately although it is easy to show a better way, it is very difficuly to communicate a better way through discussion.
    I suggest searching the net for examples of ways to avoid globals.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Those two main parts of the program do use two separate sets of variables. But each of those
    have several subroutines with each using many variables.

    I do really need to post the code. I will do so as soon as I clean it up.

    I have to warn all you though - no comments!

    On the other hand, it is quite neat and should be sufficient for the question at hand.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Am starting a new topic with source code, for advice on that particular program.

    I'll leave this one here for general info on globals.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Um, people who start several threads on ostensibly the same thing become unpopular quickly. You already told us what the program does so it's not off topic.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global scope and pointers
    By grytskiv in forum C Programming
    Replies: 1
    Last Post: 03-13-2011, 05:53 AM
  2. scope of global variable vs. static
    By chiefmonkey in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2009, 12:23 PM
  3. Global scope and passing pointer
    By Dave++ in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2007, 03:52 PM
  4. scope of global variables
    By laertius in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2006, 01:59 AM
  5. Global scope
    By Hunter2 in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 08:51 PM