在函数顶部或单独的范围内声明变量?范围内、变量、函数、声明

2023-09-07 02:52:32 作者:昔日已成往事

方法 1 还是方法 2 哪个更受欢迎?

Which is preferred, method 1 or method 2?

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;
}

方法二:

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;
}

在方法1中,如果调用wpMainWindow函数时msg = WM_PAINT,是否一开始就为栈上的所有变量分配内存?还是只有当它进入 WM_PAINT 范围时?

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?

方法一会不会只在message为WM_PAINT时才使用内存,而方法二不管msg等于什么都会使用内存吗?

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?

推荐答案

变量应该尽可能在本地声明.

Variables should be declared as locally as possible.

在函数顶部"声明变量始终是一种非常糟糕的做法.即使在 C89/90 语言中,变量只能在块的开头声明,最好尽可能在本地声明它们,即在覆盖变量所需生命周期的最小局部块的开头.有时,引入一个冗余"本地块,其唯一目的是本地化"变量声明,这甚至可能是有意义的.

Declaring variables "at the top of the function" is always a disastrously bad practice. Even in C89/90 language, where variables can only be declared at the beginning of the block, it is better to declare them as locally as possible, i.e. at the beginning of smallest local block that covers the desired lifetime of the variable. Sometimes it might even make sense to introduce a "redundant" local block with the only purpose of "localizing" the variable declaration.

在 C++ 和 C99 中,可以在代码中的任何位置声明变量,答案非常简单:再次,尽可能在本地声明每个变量,并尽可能靠近使用它的位置第一次.这样做的主要理由是,在大多数情况下,这将允许您在声明时为变量提供有意义的初始化程序(而不是在没有初始化程序或使用虚拟初始化程序的情况下声明它).

In C++ and C99, where it is possible to declare variable anywhere in the code, the answer is pretty straightforward: again, declare each variable as locally as possible, and as close as possible to the point where you use it the very first time. The primary rationale for that is that in most cases this will allow you to supply a meaningful initializer to the variable at the point of declaration (instead of declaring it without initializer or with a dummy initializer).

至于内存使用,一般一个典型的实现会立即(当你输入函数时)为同时存在的所有变量分配所需的最大空间.但是,您的声明习惯可能会影响该空间的确切大小.例如,在这段代码中

As for the memory usage, in general a typical implementation will immediately (as you enter the function) allocate the maximum space required for all variables that exist at the same time. However, your declaration habits might affect the exact size of that space. For example, in this code

void foo() {
  int a, b, c;

  if (...) {
  }

  if (...) {
  }
}

所有三个变量同时存在,通常必须为所有三个变量分配空间.但是在这段代码中

all three variables exist at the same time and generally the space for all three has to be allocated. But in this code

void foo() {
  int a;

  if (...) {
    int b;
  }

  if (...) {
    int c;
  }
}

在任何给定时刻只存在两个变量,这意味着典型实现将只为两个变量分配空间(bc 将共享相同的空间).这是尽可能在本地声明变量的另一个原因.

only two variables exist at any given moment, meaning that space for only two variables will be allocated by a typical implementation (b and c will share the same space). This is another reason to declare variables as locally as possible.