Here is some code that I copied from the Microsoft Developer Network
http://msdn.microsoft.com/en-us/library/dd162487(v=VS.85).aspx
LRESULT APIENTRY WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
**PAINTSTRUCT ps;
HDC hdc;**
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
TextOut(hdc, 0, 0, "Hello, Windows!", 15);
EndPaint(hwnd, &ps);
return 0L;
// Process other messages.
}
}
I am probably wrong but I thought that everytime the compiler ran a statement like any one of these:
int var1
double var2
char var3[]
PAINTSTRUCT ps
HDC hdc
the computer would create a new variable. At least that would be the logical thing to think because that is what you write when you want to create a new variable, right?
I also always thought that if you had a block of code like this:
for(int i = 0; i < 100; i++)
int sum = i;
the computer would create 100 different variables all with the same name sum
and some value that was contained in i
In the sample code above, the function WndProc will be called many many times over the course of the program, however the two variables that the function creates called "ps" and "hdc" will only be used some of the times that the function executes.
So would the computer be making lots of separate, extra PAINTSTRUCT and HDC variables that it will never use?
Would it not be at least slightly more efficiant to declare "ps" and "hdc" after case WM_PAINT: like this?
case WM_PAINT:
{
**PAINTSTRUCT ps;
HDC hdc;**
hdc = BeginPaint(hwnd, &ps);
TextOut(hdc, 0, 0, "Hello, Windows!", 15);
EndPaint(hwnd, &ps);
}
return 0L;