Static or global in this case?

This is a discussion on Static or global in this case? within the Windows Programming forums, part of the Platform Specific Boards category; I'm making a base framework for an OpenGL game I'm making for Windows, I'm currently using the HWND, HDC and ...

  1. #1
    myNegReal
    Join Date
    Jun 2005
    Posts
    100

    Static or global in this case?

    I'm making a base framework for an OpenGL game I'm making for Windows, I'm currently using the HWND, HDC and HGLRC as static variables in WinMain, so I have to pass them to other functions if I want to use them. Would it be better to make the global so I don't have to pass them or should I leave them static? Also, what is so bad about global variables besides that they take up memory right at the beginning of the program? Statics also always have memory allocated so does it matter?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    It's often more a question of taste. Global variables can be helpful in many situations as they avoid the need to pass variables across function calls (sometimes this is just convinient, other times it may give a slight speed advantage), but they can sometimes lead to problems, especially for larger projects. It's better to couple variables with the functions that use them

  3. #3
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Ditto what Fordy said. In addition, it is generally considered bad programming practice to have global variables/objects, however, there are some situations where you have no alternative (for example, in an MFC app you _must_ have a global CWinApp object), but if you aren't forced to, you should, at a bare minimum, do whatever you can to keep your variables restricted in scope. Even some built-in global objects are restricted in scope via exploitation of namespaces (consider cin and cout, for example). You should never make a variable global to avoid the inconvenience of passing them across scopes. That is not only a poor practice but takes you to a lower level of lazy programming.

  4. #4
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    There are many times when global variables are quite useful, in my opinion windows is a great example because it really offers no decent way of interprocess communication because it lacks signals. Windows messages can't really be compared because as far as I know you can't send a message to a process and you can't enumerate windows from a process id. Globals and shared memory really help (though still less powerful in my opinion); globals are 'bad programming' because it is rather opposite of encapsulation which people have come to expect when expanding pre-written code or using a library.
    I believe some people will do stupid inneficient things just to avoid using a global variable or an unconditional jump, but both have their place. Use a global variable if it is convenient and will simplify the code, pass it to a function if that would make the function general, just don't go out of your way to avoid something that really makes sense.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    87
    because it really offers no decent way of interprocess communication because it lacks signals
    I can't agree with valis.
    Windows offers plenty of methods for interprocess comm. There are pipes- named and unnamed, mailslots, simple message for GUI-apps, I think it was WM_COPYDATA or something like this, shared memory, sockets, Windows clipboard and many other like COM and so on(there could be some that I don't know about).

    Can you name so many means of interprocess communication under any other OS?
    Last edited by Gravedigga; 07-14-2005 at 04:35 PM.

  6. #6
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    I think I worded that wrongly, I meant it has no great event driven means of communication, they offer a very small set of signals simply for compatability, but those are rather useless, one could argue messages are parallel, but I find them to be less powerful. Sockets, pipes, and shared memory are standard for a full fledged OS.
    I won't argue that windows is a great os, as are the various unix and linux flavors, but I do find some frustration when confronted with a problem that could be easily solved by signals.

    In any case, I think for things like thread communication, a simple global can greatly decrease complexity.

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    >>I meant it has no great event driven means of communication,

    Look at HWND_BROADCAST msgs

    Allows you to send custom defined msgs between applications.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. Intel syntax on MinGW ?
    By TmX in forum Tech Board
    Replies: 2
    Last Post: 01-06-2007, 08:44 AM
  4. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  5. rand()
    By serious in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 01:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21