Thread: An Elementary Windows Question

  1. #1
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382

    An Elementary Windows Question

    A strange question, but where do you put your main variables for a Windows program? In a standard console app, they would go in main (), but you can't really put them in WinMain () because the WindowProc handles most of the code.

    But it also doesn't seem appropriate to me to put all the variables in a callback function so I tend to cheat and just make them global.

    So where is it conventional to put them?
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    If the variables are only used in one function such as WinMain then put them in WinMain, if you want them to retain their values use the static keyword.
    Global variables should be used when you want them to be accessable from several functions.

  3. #3
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    It's just that I have a project with some large structures (that are global for now) that need to be accessed by the window message handler and several other functions.

    Should I declare them in the message procedure then pass them to the other functions from there?
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    You should probably leave them as global.

  5. #5
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Would that make the program significantly less efficient?
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Global variables stay in memory where as local variables are created when the function is called. If these variables are in a function that is called offen then it isn't less efficant in terms of memory usage to make them global.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by samGwilliam
    A strange question, but where do you put your main variables for a Windows program? In a standard console app, they would go in main (), but you can't really put them in WinMain () because the WindowProc handles most of the code.

    But it also doesn't seem appropriate to me to put all the variables in a callback function so I tend to cheat and just make them global.

    So where is it conventional to put them?
    Why doesn't it seem appropiate? I put them in WndProc all the time then I just pass them to pointers. Globals cluttering up your global namspace is bad, it'll make it harder to manage your program once it's a decent size and it introduces bugs. If you need to retain the same values throughout function calls, either make your variables static or allocate them on the heap.


    xeddiex.

  8. #8
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    In the past, I've created a C struct that contained all of the otherwise global data, and then either passed around the pointer (or a handle) to the instantiated structure or made the pointer to the structure (or the structure itself) global. That way you're not cluttering up the namespace and every access to the members of this structure are called out by the structure access ("context.member" or "context->member").

    I don't know if this is what you're looking for or not. If the structure itself is global, the run-time performance is exactly that of separate global variables, as the dot accessor resolves to a memory address rather than a pointer and offset.
    Insert obnoxious but pithy remark here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-16-2006, 02:05 PM
  2. Two Windows on two different HDD??? Question
    By Sevrin in forum Tech Board
    Replies: 6
    Last Post: 06-19-2003, 10:22 AM
  3. Codec Bitrates?
    By gvector1 in forum C# Programming
    Replies: 2
    Last Post: 06-16-2003, 08:39 AM
  4. FlashWindowEx not declared?
    By Aidman in forum Windows Programming
    Replies: 3
    Last Post: 05-17-2003, 02:58 AM
  5. Another windows service question... starting/stopping
    By BrianK in forum Windows Programming
    Replies: 1
    Last Post: 03-20-2003, 12:22 AM