Should Local Variable Initialisation Be Mandatory?

前端 未结 17 2490
忘掉有多难
忘掉有多难 2021-02-14 01:26

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 se

相关标签:
17条回答
  • 2021-02-14 02:01

    Just a secondary observation. Initializations are only EASILY optimized on primitive types or when assigned by const functions.

    a= foo();

    a= foo2();

    Cannot be easily optimized because foo may have side effects.

    Also heap allocations before time might result in huge performance hits. Take a code like

    void foo(int x)
    

    {

    ClassA *instance= new ClassA();

    //... do something not "instance" related... if(x>5) {

    delete instance;
    
    return;
    

    }

    //.. do something that uses instance

    }

    On that case, simply declare instance just when you will use it, and initialize it only there. And no The compiler Cannot optimize that for you since the constructor may have side effects that code reordering would change.

    edit: I fail at using the code listing feature :P

    0 讨论(0)
  • 2021-02-14 02:02

    If you think that an initialization is redundant, it is. My goal is to write code that is as humanly readable as possible. Unnecessary initialization confuses future reader.

    C compilers are getting pretty good at catching usage of unitialized variables, so the danger of that is now minimal.

    Don't forget, by making "fake" initialization, you trade one danger - crashing on using garbage (which leads to a bug that is very easy to find and fix) on another - program taking wrong action based on fake value (which leads to a bug that is very difficult to find). The choice depends on the application. For some, it is critical never to crash. For majority, it is better to catch the bug ASAP.

    0 讨论(0)
  • 2021-02-14 02:03

    I think it is in most cases a bad idea to initialize variables with an default value, because it simply hides bugs, that are easily found with uninitialized variables. If you forget to get and set the actual value, or delete the get code by accident, you probably never notice it because 0 is in many cases a reasonable value. Mostly it is much easier to trigger those bugs with an value >> 0.

    For example:

    
    void func(int n)
    {
        int i = 0;
    
        ... // Many lines of code
    
        for (;i < n; i++)
            do_something(i);
    

    After some time you are going to add some other stuff.

    
    void func(int n)
    {
        int i = 0;
    
        for (i = 0; i < 3; i++)
            do_something_else(i);
    
        ... // Many lines of code
    
        for (;i < n; i++)
            do_something(i);
    

    Now your second loop won't start with 0, but with 3, depending on what the function does it can be very difficult to find, that there is even a bug.

    0 讨论(0)
  • 2021-02-14 02:04

    As you've showed with respect to performacne it does not make a difference. The compiler will (in optimized builds) detect if a local variable is written without beeing read from and remove the code unless it has other side-effects.

    That said: If you initialize stuff with simple statements just to be sure it's initialized it's fine to do so.. I personally don't do it, for a single reason:

    It tricks the guys who may later maintain your code into thinking that the initialization is required. That little foo = 0; will increase the code-complexity. Other than that it's just a matter of taste.

    If you unnessesary initialize variables via complex statements it may have a side-effect.

    For example:

      float x = sqrt(0);
    

    May be optimized by your compiler if you are lucky and work with a clever compiler. With a not so clever compiler it may as well result in a costly and unnessesary function-call because sqrt can - as a side-effect - set the errno variable.

    If you call functions that you have defined yourself my best bet is, that the compiler always assumes that they may have side-effects and don't optimize them out. That may be different if the function happen to be in the same translation unit or you have whole program optimization turned on.

    0 讨论(0)
  • 2021-02-14 02:05

    I'm not sure if it is necessary to "make them mandatory", but I personally think it is always better to initialize variables. If the purpose of the application is to be as tight as possible then C/C++ is open for that purpose. However, I think many of us have been burned a time or two by not initializing a variable and assuming it contains a valid value (e.g. pointer) when it really doesn't. A pointer with an address of zero is much easier to check for than if it has random garbage from the last memory contents at that particular location. I think in most cases, it is no longer a matter of performance but a matter of clarity and safety.

    0 讨论(0)
  • 2021-02-14 02:05

    Performance? Nowadays? Maybe back when CPUs ran at 10mhz it did make sense, but today its hardly a problem. Always initialise them.

    0 讨论(0)
提交回复
热议问题