Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 320 Vote(s) - 3.68 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Declare variables at top of function or in separate scopes?

#1
Which is preferred, method 1 or method 2?

### Method 1:

LRESULT CALLBACK wpMainWindow(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_PAINT:
{
HDC hdc;
PAINTSTRUCT ps;

RECT rc;
GetClientRect(hwnd, &rc);

hdc = BeginPaint(hwnd, &ps);
// drawing here
EndPaint(hwnd, &ps);
break;
}
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
}
return 0;
}

### Method 2:

LRESULT CALLBACK wpMainWindow(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rc;

switch (msg)
{
case WM_PAINT:
GetClientRect(hwnd, &rc);

hdc = BeginPaint(hwnd, &ps);
// drawing here
EndPaint(hwnd, &ps);
break;

default:
return DefWindowProc(hwnd, msg, wparam, lparam);
}
return 0;
}

In method 1, if msg = WM_PAINT when wpMainWindow function is called, does it allocate memory for all the variables on the stack at the beginning? or only when it enters the WM_PAINT scope?

Would method 1 only use the memory when the message is WM_PAINT, and method 2 would use the memory no matter what msg equaled?
Reply

#2
Memory allocation is not specified in the Standard to this detail, so for a real answer you'll have to specify compiler and platform. It's not going to matter for performance.

What you want is readability, and in general that's done by declaring variables in the smallest usable scope, and preferably when you can immediately initialize them with reasonable values. The smaller a variable's scope, the less it can potentially interact with the rest of the program in unpredictable ways. The closer the declaration to initialization, the less opportunity for anything bad to happen.

What would probably be better is something like

RECT rc;
GetClientRect(hwnd, &rc);
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);

This is for C++. For C, the rule is similar, except that earlier versions of C required all the variables to be declared at the top of a block.
Reply

#3
You can't know at what point the stack reservation is done.

For readability I would go with C99 (or C++). That allows you the declaration of a variable really there where first use it.

HDC hdc = BeginPaint(hwnd, &ps);

Reply

#4
Define the variables in the narrowest scope where they are relevant. There's no reason to use Method 2 above in my opinion.

Stack space is only likely to be used when the variables are in scope. As @paxdiablo points out, your locals may wind up in registers rather than on the stack, if the compiler can find the space for them.
Reply

#5
Whether something's allocated on the stack in case 1 is implementation defined. Implementations aren't even _required_ to have a stack.

It's usually no _slower_ to do so since the operation tends to be a simple subtraction (for a downward growing stack) of one value from the stack pointer for the entire local variable area.

The thing that's important here is that the scope should be as local as possible. In other words, declare your variables as late as possible and only keep them around as long as needed.

Note that declaring here is at a different abstraction level to allocating space for them. The actual space may be allocated at the start of the function (implementation level) but you can only use those variables while they're scoped (C level).

Locality of information is important, just like its cousin, encapsulation.
Reply

#6
Since it's the compiler's job to optimize my code, and an hour of compiler-time is way cheaper than an hour of my time, and my time gets wasted if I need to scroll up and down the code to see where a variable was declared, I think my company wants me to keep everything as local as possible.

Not even am I talking about 'the smallest block', but 'as near to the place where it is used'!

LRESULT CALLBACK wpMainWindow(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_PAINT:
{
RECT rc;
GetClientRect(hwnd, &rc);

{ // sometimes I even create an arbitrary block
// to show correlated statements.
// as a side-effect, the compiler may not need to allocate space for
// variables declared here...
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// drawing here
EndPaint(hwnd, &ps);
}
break;
}
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
}
return 0;
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through