Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9173193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:31:56+00:00 2026-06-17T16:31:56+00:00

it is acceptable to declare/define a variable in the WndProc function. as this code:

  • 0

it is acceptable to declare/define a variable in the WndProc function. as this code:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;

switch (message)
{
...

the question whether it’s good.
Because the program enters into this function, thousands of times every minute, and every time these variables are redefined, and again destroyed, maybe it is better to define these variables outside the WndProc function, in the class, thus saving time and resources?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:31:57+00:00Added an answer on June 17, 2026 at 4:31 pm

    Because the program enters into this function, thousands of times every minute, and every time these variables are redefined, and again destroyed, maybe it is better to define these variables outside the WndProc function, in the class, thus saving time and resources?

    As far as time and resources go, it makes little difference. These are local variables, stored on the stack. The entire stack is already reserved when your thread is created, and the memory for these variables is committed no later than the first time this procedure is executed. So this means that, for switch conditions that don’t refer to these variables, you likely won’t be able to measure the difference in runtime or resource needs caused by the presence of the variables.

    Raymond points out that WndProc can be called recusively. If you have a lot of local variables in your WndProc, the resulting stack usage can be sub-optimal. I still can’t imagine any real world application where the bottleneck was stack usage of WndProc.

    Having said that, a WndProc method which is one big switch statement containing all the variables is bad practice. You don’t want to pollute the local namespace of this function with lots of different variables. I suggest you make a separate handler function for each message that you want to handle in the window procedure.

    Finally, you suggest declaring these variables in the class. Don’t do this. Local variables are always preferred where possible. It’s certainly going to be possible to declare these variables as locals. Although they should be locals of the separate handler functions that WndProc calls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is the following code acceptable. That is, is this the right way to do
I'm wondering what is the culturally-acceptable way to handle this code situation. I have
To keep the global namespace clean, my JavaScript code is wrapped like this: (function()
Is the following always acceptable code? It seems to work, but does it consistently
Is this an okay practice or an acceptable way to use PHP's error suppressing?
I'm just curious if the community considers it acceptable to use the term Code
Ok... I have this struct and comparison function- struct Edge { char point1; char
I was bug-fixing some code and the compiler warned (legitimately) that the function dynscat()
Is it acceptable for interfaces to declare properties instead of methods? Which is more
Being new to PHP, and with a lot riding on this function wrapper, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.