Initializing variables in C

后端 未结 10 692
长情又很酷
长情又很酷 2020-12-02 12:21

I know that sometimes if you don\'t initialize an int, you will get a random number if you print the integer.

But initializing everything to zero seems

相关标签:
10条回答
  • 2020-12-02 13:15

    I can think of a couple of reason off the top of my head:

    1. When you're going to be initializing it later on in your code.

      int x;
      
      if(condition)
      {
          func();
          x = 2;
      }
      else
      {
         x = 3;
      }
      anotherFunc(x); // x will have been set a value no matter what
      
    2. When you need some memory to store a value set by a function or another piece of code:

      int x;  // Would be pointless to give x a value here
      scanf("%d", &x);
      
    0 讨论(0)
  • 2020-12-02 13:16

    In general, there's no need to initialize a variable, with 2 notable exceptions:

    1. You're declaring a pointer (and not assigning it immediately) - you should always set these to NULL as good style and defensive programming.
    2. If, when you declare the variable, you already know what value is going to be assigned to it. Further assignments use up more CPU cycles.

    Beyond that, it's about getting the variables into the right state that you want them in for the operation you're going to perform. If you're not going to be reading them before an operation changes their value (and the operation doesn't care what state it is in), there's no need to initialize them.

    Personally, I always like to initialize them anyway; if you forgot to assign it a value, and it's passed into a function by mistake (like a remaining buffer length) 0 is usually cleanly handled - 32532556 wouldn't be.

    0 讨论(0)
  • 2020-12-02 13:21

    There are several circumstances where you should not initialize a variable:

    1. When it has static storage duration (static keyword or global var) and you want the initial value to be zero. Most compilers will actually store zeros in the binary if you explicitly initialize, which is usually just a waste of space (possibly a huge waste for large arrays).
    2. When you will be immediately passing the address of the variable to another function that fills its value. Here, initializing is just a waste of time and may be confusing to readers of the code who wonder why you're storing something in a variable that's about to be overwritten.
    3. When a meaningful value for the variable can't be determined until subsequent code has completed execution. In this case, it's actively harmful to initialize the variable with a dummy value such as zero/NULL, as this prevents the compiler from warning you if you have some code paths where a meaningful value is never assigned. Compilers are good at warning you about accessing uninitialized variables, but can't warn you about "still contains dummy value" variables.

    Aside from these issues, I'd say it's generally good practice to initialize your non-static variables when possible.

    0 讨论(0)
  • 2020-12-02 13:21

    There is absolutely no reason why variables shouldn't be initialised, the compiler is clever enough to ignore the first assignment if a variable is being assigned twice. It is easy for code to grow in size where things you took for granted (such as assigning a variable before being used) are no longer true. Consider:

    int MyVariable;
    void Simplistic(int aArg){
        MyVariable=aArg;
    }
    
    //Months later:
    
    int MyVariable;
    void Simplistic(int aArg){
        MyVariable+=aArg; // Unsafe, since MyVariable was never initialized.
    }
    

    One is fine, the other lands you in a heap of trouble. Occasionally you'll have issues where your application will run in debug mode, but release mode will throw an exception, one reason for this is using an uninitialised variable.

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