在功能顶部或单独的范围中声明变量? [英] Declare variables at top of function or in separate scopes?

查看:80
本文介绍了在功能顶部或单独的范围中声明变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是preferred,方法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;
}

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

在方法1,如果味精= WM_PAINT时wpMainWindow函数被调用,它的堆栈开头的所有变量分配内存?或者当它进入只有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?

应该方法时,消息WM_PAINT 1只使用内存和方法2将不管什么持平味精使用的内存?

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,它可以在code随地声明变量,答案是pretty简单:再一次,为本地声明每个变量越好,尽可能接近的地步在您使用它的第一次。对于主要的理由是,在大多数情况下,这将让你在声明的时候提供一个有意义的初始化的变量(而不是没有初始化或者用虚拟初始化声明它)。

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).

至于内存使用情况,在一般典型的实现将立即(作为输入的功能)分配为存在同时所有变量所需的最大空间。然而,你的宣言的习惯可能会影响空间的确切大小。例如,在此code

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 (...) {
  }
}

所有三个变量同时存在,并通常为所有三个空间具有将被分配。但是在这个code

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

只有两个变量在任何特定时刻存在,这意味着只有两个变量的空间将被一个典型的实现( B C <分配/ code>将共享相同的空间)。这是另一个原因,以尽可能在本地声明变量。

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.

这篇关于在功能顶部或单独的范围中声明变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆