reducing global variables in windows programs

This is a discussion on reducing global variables in windows programs within the Windows Programming forums, part of the Platform Specific Boards category; God, i've got this fussy feeling all over me... Just to point out (yeah, i see your point about having ...

  1. #16
    Registered User VBprogrammer's Avatar
    Join Date
    Mar 2002
    Posts
    175
    God, i've got this fussy feeling all over me...

    Just to point out (yeah, i see your point about having debates about these type of things) that to use SetWindowLong(hWnd, GWL_USERDATA, lParam) which is an API call your computer must use a far pointer to a call gate which isn't particularly fast. Not something you should worry about too much, however for a global value which changes very often you should definatly use a global varrible. Obviosly storing things like the window handle wouldn't be affected by this.

    Yeah and i know i should change my name but that would confuse everyone on Flashdaddee...
    VC++ 6

  2. #17
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    that to use SetWindowLong(hWnd, GWL_USERDATA, lParam) which is an API call your computer must use a far pointer to a call gate which isn't particularly fast.
    aren't all pointers in Win32 FAR? actually, isn't FAR defined as nothing now? I know it meant a lot in Win16 so I'm not trying to show my brilliance here.
    for a global value which changes very often you should definatly use a global varrible
    if you are multithreaded this will kill your program. I had to explain this to my co-worker who is an ancient Win16 guy. He had this wonderful way of using a global char string for EVERYTHING that he needed a string for. Great in the old days but with two threads using the same string for two different purposes? WOOooohhh Das bad!

  3. #18
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Hershlag's right. Globals may be o.k. for single-threaded apps (if not sloppy), but for multithreaded they could spell doom for yer program. Even if you resorted to a critical-section, that would mean defining yet another global variable (unless you used a Win32 mutex/semaphore, of course)!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #19
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by Hershlag

    aren't all pointers in Win32 FAR? actually, isn't FAR defined as nothing now? I know it meant a lot in Win16 so I'm not trying to show my brilliance here.
    Yup...all windows pointers are 32bit addresses......that's all that's needed as windows processes sit in a flat address space

    In 1 header I saw FAR defined as

    #define FAR

    so it doesnt do much

  5. #20
    Registered User Compuboy's Avatar
    Join Date
    Apr 2002
    Posts
    16

    VBprogrammer

    &nbsp;&nbsp;VBprogrammer, am I right in thinking that the "VB" in your name stands for "Very Bad", and not "Visual Basic"? I just seem to recall you telling me that at some point.

    &nbsp;&nbsp;Oh, by the way, I can identify with both the weel-reinventors and the cart-makers! I go through programming "moods". Sometimes I prefer ASM, sometimes I prefer C/C++. It also depends a lot on the project I happen to be working on at the time. I tend to use ASM whenever I feel that it would significantly increase the speed.

  6. #21
    Registered User VBprogrammer's Avatar
    Join Date
    Mar 2002
    Posts
    175
    Yeah, Sorry i got my MMURTL and Windows wires crossed up (in MMURTL the OS is in a different segment to the user code which means FAR pointers are required). I plead temporary insanity.

    However you might want to chew on the fact that with only one processor only one task can be exicuting at a time, therefore sencible use of global varribles should not be a problem. What you are talking about is code re-entracy where if a thread is updating something critical (i.e. a linked list) then the other process comes along and tries use the same thing it would cause big problems. There have to be measures taken to prevent problems like this occuring. In windows this takes the form of custom (WM_USER based) messages.

    Compuboy:: shhh! That depends on how stupid my previous comments have been...

    Oh yeah, just remember without us low-levelers you high levelers wouldn't have a leg to stand on!
    VC++ 6

  7. #22
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by VBprogrammer
    However you might want to chew on the fact that with only one processor only one task can be exicuting at a time, therefore sencible use of global varribles should not be a problem. What you are talking about is code re-entracy where if a thread is updating something critical (i.e. a linked list) then the other process comes along and tries use the same thing it would cause big problems. There have to be measures taken to prevent problems like this occuring. In windows this takes the form of custom (WM_USER based) messages.
    No there are definately problems when you have more than 1 thread accessing global data...and it has nothing to do with messages....windows offers no guarantee that a piece of code will execute fully without being pre-empted by another thread.....its true that 1 processor can only run 1 thread at a time, but the allocation of clock cycles does not run how you would logically expect and you cant simply rely on it.

    Windows offers various messures for you to guard global data...for instance the CRITICAL_SECTION..Used properly this will give a guarantee that 1 thread will complete a task with reagrd to global data before being pre-empted...for more info, look up Jeff Richter's book - he gives a whole chapter to thread syncronisation.....

  8. #23
    Registered User VBprogrammer's Avatar
    Join Date
    Mar 2002
    Posts
    175
    Yeah I know what pre-emptive multitasking is Fordy.
    VC++ 6

  9. #24
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    My favorite programming phrase. "Global is evil"

    the thing about critical sections is they should be used sparingly. They switch you back to sort of a procedural mode and there's no point in being multithreaded!

  10. #25
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    the thing about critical sections is they should be used sparingly. They switch you back to sort of a procedural mode and there's no point in being multithreaded!


    Yes, there is! Threads make a program run smoother, and allow you to do what just isn't possible otherwise. Also, it is simple to create a class that encapsulates all of the the "procedural" stuff, critical sections and all...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #26
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    Can I take this opportunity to ask about static variables please?

    I am creating my first WinAPI program in the form of a game of battleships. I have created a grid of square bitmaps and I wish the user to click on the square he/she wishes to fire at and then click a button to fire. When the user clicks on the square, the WM_LBUTTONDOWN message is handled and the coordinates of the cursor position are stored in a globally defined integer for use by WndProc() once the user clicks the "Fire" button. If this integer were declared inside WndProc() as a static integer would it have the same effect (i.e. contain the data given to it during the last WndProc() call)? What about if (as I often do) I initialise the integer to 0 at the beginning of WndProc()? Will it revert to 0 every time WndProc() is called?

    Thanks

  12. #27
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    Originally posted by Sebastiani


    Yes, there is! Threads make a program run smoother, and allow you to do what just isn't possible otherwise. Also, it is simple to create a class that encapsulates all of the the "procedural" stuff, critical sections and all...
    I think you missed my point. "Sparingly" doesn't mean not at all. For the time you are in a critical section you are not multithreaded. That's what I meant. Overuse of them will eliminate the multi-threadedness of your app. das all

  13. #28
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    Originally posted by minesweeper
    Can I take this opportunity to ask about static variables please?

    I am creating my first WinAPI program in the form of a game of battleships. I have created a grid of square bitmaps and I wish the user to click on the square he/she wishes to fire at and then click a button to fire. When the user clicks on the square, the WM_LBUTTONDOWN message is handled and the coordinates of the cursor position are stored in a globally defined integer for use by WndProc() once the user clicks the "Fire" button. If this integer were declared inside WndProc() as a static integer would it have the same effect (i.e. contain the data given to it during the last WndProc() call)? What about if (as I often do) I initialise the integer to 0 at the beginning of WndProc()? Will it revert to 0 every time WndProc() is called?

    Thanks
    I think you are referring to the initialization of the static as in:
    Code:
    static int coord_x = 0;
    static int coord_y = 0;
    and the answer is nope. Only when the program first starts. Static in this case is much like a global in that it exists for the length of the program. The initialization does not happen every time in.

  14. #29
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I think you missed my point. "Sparingly" doesn't mean not at all. For the time you are in a critical section you are not multithreaded.
    Ok. I don't mean to be nitpicky but I just want to add that critical sections do not affect the multithreadedness of an app. Well, ok, I think I see your point now. In a sense they DO put the threads that require the same data into a sort of "procedural" processsion...Nevermind Anyway, in a lot of cases, separate threads interact very little. That's all. I'll shaddup now
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  15. #30
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    Sebastiani, at least YOU understand threads and their importance to windows. That's more than I can say for SOME V(ery)B(ad)Programmers. Just kidding VB

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  2. General global bool variables?
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 02-27-2005, 09:16 PM
  3. Headers and Global Variables
    By ajoo in forum C++ Programming
    Replies: 1
    Last Post: 05-15-2004, 05:49 AM
  4. Replies: 6
    Last Post: 01-02-2004, 01:01 PM
  5. Global variables.. how bad?
    By punkrockguy318 in forum C++ Programming
    Replies: 19
    Last Post: 11-30-2003, 10:53 PM

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