Thread: Avoiding Global variables

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    I've learned at school that i should avoid Global declarations... But now with windows API i noticed all variables to be used or updated in WM_PAINT after every WM_COMMAND need to be global, otherwise won't update... Is there a limit here? and is my perception correct
    They don't need to be global - they just need to be the same variable. Local variables in a function are "recreated" every time the function is called.

    You have local variables that are static, which means that the variable remains each time.

    There are also, usually, many other ways to have local but persistent storage - the typical way is to pass something from a "never ending" function to the lower level function, which is some sort of struct that contains your actual "global" variables.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Also, dont listen too much to the 'no global variables' camp. While you should try to minimize the number you use, they are a tool to be used as necessary. One of the issues with global variables is that you can take 2 perfectly legitimate cpp files from different projects, put them into a new project and get compile errors because they happen to use the same name for a global variable. Its also kind of a pain to extern alot if you use too many global variables, but there are some times when its just too simple a solution not to use one. Like most things in life, listen to the advice of those who have gone before, but don't take it as gospel.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    They don't need to be global - they just need to be the same variable. Local variables in a function are "recreated" every time the function is called.

    You have local variables that are static, which means that the variable remains each time.

    There are also, usually, many other ways to have local but persistent storage - the typical way is to pass something from a "never ending" function to the lower level function, which is some sort of struct that contains your actual "global" variables.

    --
    Mats
    Mats i declare a variable to contain a handle to an edit control inside the WinProc, initialized within WM_CREATE. But i intend to use this variable within WM_PAINT and would sometimes refresh (call InvalidateRect) WM_PAINT.. I noticed that i don't get it initialized properly unless i move its declaration globally. So you suggest i should declare it as static rather within WinProc? There are few others which would need same resolution

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Like Abachler, I don't have a religious conviction that global variables are all bad. They are, to again echo Abachler, a tool. A hammer is a tool - if you use it incorrectly your thumb, or the walls in your house, may get damaged. But use it correctly with nails, and you can build a house. A knife can kill people, but it's also a useful tool to cut up your meat or vegetables to make your dinner.

    So, used with care and for the right reasons, global variables are "OK". There are a few problems with global variables:
    1. It's hard to know when and where they get changed.
    2. There is a risk of name collisions when merging source from different places.
    3. It is harder to change the behaviour of the code if global variables are used too much - for example if you want to create another of something, it's quite easy when it's being passed around as a parameter to various functions, but if it's one global variable, you need to create another variable and then go around and change all the code that uses the first variable to KNOW which one to use at what times.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by csonx_p View Post
    Mats i declare a variable to contain a handle to an edit control inside the WinProc, initialized within WM_CREATE. But i intend to use this variable within WM_PAINT and would sometimes refresh (call InvalidateRect) WM_PAINT.. I noticed that i don't get it initialized properly unless i move its declaration globally. So you suggest i should declare it as static rather within WinProc? There are few others which would need same resolution
    And you are using C?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    And you are using C?
    Yes, supposedly... Well my files are .cpp (whilst writing c code) cause i can't compile .c files successfully in Visual Studio 2005 standard Edition...

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Yes, supposedly... Well my files are .cpp (whilst writing c code) cause i can't compile .c files successfully in Visual Studio 2005 standard Edition...
    Why not - Visual Studio (all versions, including 2008) can compile C code. Presumably you are either using C++ features, or something else is wrong in your code... What error(s) do you get?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Why not - Visual Studio (all versions, including 2008) can compile C code. Presumably you are either using C++ features, or something else is wrong in your code... What error(s) do you get?

    --
    Mats
    I just renamed the .cpp file to .c and hit compile, I get a pre-compile header error... turning them of gives billion errors

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    I just renamed the .cpp file to .c and hit compile, I get a pre-compile header error... turning them of gives billion errors
    Yeah, you probably need to do a clean before you build (rebuild all or whatever it's called in MS speak), as the precompiled headers will "know" whether you are using C++ or C when compiling the header, so if you just rename the file, it will still think the precompiled header is the same [my personal opinion is that precompiled headers are only worth-wile in big projects - in small projects the saving is pretty small, really].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-06-2008, 09:59 AM
  2. global variables
    By shadovv in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2005, 02:21 PM
  3. global variables - okay sometimes...?
    By MadHatter in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2003, 04:23 PM
  4. global variables
    By rdnjr in forum Linux Programming
    Replies: 0
    Last Post: 01-07-2003, 10:28 AM
  5. Global variables? Bad! Yes, but to what extent?
    By Boksha in forum C++ Programming
    Replies: 6
    Last Post: 05-26-2002, 04:37 PM