如果局部变量初始化是强制性的? [英] Should Local Variable Initialisation Be Mandatory?

查看:112
本文介绍了如果局部变量初始化是强制性的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是未初始化的当地人造成(尤其是三分球)的维修问题将是显而易见的人谁做了一些的C / C ++的维护或增强,但我仍然看到他们偶尔能听到给出自己的理由性能影响。

The maintenance problems that uninitialised locals cause (particularly pointers) will be obvious to anyone who has done a bit of c/c++ maintenance or enhancement, but I still see them and occasionally hear performance implications given as their justification.

这很容易在C证明,冗余初始化优化了:

It's easy to demonstrate in c that redundant initialisation is optimised out:

$ less test.c
#include <stdio.h>
main()
{
#ifdef INIT_LOC
    int a = 33;
    int b;
    memset(&b,66,sizeof(b));
#else
    int a;
    int b;
#endif
    a = 0;
    b = 0;
    printf ("a = %i, b = %i\n", a, b);
}

$ gcc --version
gcc (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)

[未优化:]

[Not Optimised:]

$ gcc test.c -S -o no_init.s; gcc test.c -S -D INIT_LOC=1 -o init.s; diff no_in
it.s init.s
22a23,28
>       movl    $33, -4(%ebp)
>       movl    $4, 8(%esp)
>       movl    $66, 4(%esp)
>       leal    -8(%ebp), %eax
>       movl    %eax, (%esp)
>       call    _memset
33a40
>       .def    _memset;        .scl    3;      .type   32;     .endef

[优化:]

$ gcc test.c -O -S -o no_init.s; gcc test.c -O -S -D INIT_LOC=1 -o init.s; diff
 no_init.s init.s
$

那么什么情况下WRT性能是强制性的变量初始化不是一个好主意?

So WRT performance under what circumstances is mandatory variable initialisation NOT a good idea?

如果适用,没有必要限制答案C / C ++,但请清楚的语言/环境(和可重复的证据太多preferred在炒作!)

IF applicable, no need to restrict answers to c/c++ but please be clear about the language/environment (and reproducible evidence much preferred over speculation!)

推荐答案

简短的回答:变量声明为接近尽可能先用和初始化为零,如果你仍然需要

Short answer: declare the variable as close to first use as possible and initialize to "zero" if you still need to.

龙回答:如果您在声明一个函数的开始一个变量,并且不使用它,直到后来,你应该重新考虑你的变量的位置,以当地一个范围尽可能。然后,您通常可以分配给它的价值需要马上

Long answer: If you declare a variable at the start of a function, and don't use it until later, you should reconsider your placement of the variable to as local a scope as possible. You can then usually assign to it the needed value right away.

如果您必须声明它未初始化,因为它被分配在一个条件,或按引用传递和分配,初始化为空当量值是一个好主意。编译器有时可以节省你,如果你-Wall下进行编译,因为如果你在初始化之前从变量读它会发出警告。但是,它失败,如果你把它传递给一个函数来警告你。

If you must declare it uninitialized because it gets assigned in a conditional, or passed by reference and assigned to, initializing it to a null-equivalent value is a good idea. The compiler can sometimes save you if you compile under -Wall, as it will warn if you read from a variable before initializing it. However, it fails to warn you if you pass it to a function.

如果你发挥它的安全,它设置为空当量,你做任何伤害,如果你通过它的功能覆盖它。但是,如果你将它传递给使用值的功能,你可以pretty多少保证失败的断言(如果有的话),或者至少你段错误使用空对象的第二位。随机初始化可以做各种坏事,包括工作。

If you play it safe and set it to a null-equivalent, you have done no harm if the function you pass it to overwrites it. If, however, the function you pass it to uses the value, you can pretty much be guaranteed failing an assert (if you have one), or at least segfaulting the second you use a null object. Random initialization can do all sorts of bad things, including "work".

这篇关于如果局部变量初始化是强制性的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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