Is declaration of variables expensive?

前端 未结 12 967
长情又很酷
长情又很酷 2021-02-01 00:39

While coding in C, I came across the below situation.

int function ()
{
  if (!somecondition) return false;

  internalStructure  *str1;
  internalStructure *str         


        
相关标签:
12条回答
  • 2021-02-01 00:41

    In C, I believe all variable declarations are applied as if they were at the top of the function declaration; if you declare them in a block, I think it's just a scoping thing (I don't think it's the same in C++). The compiler will perform all optimizations on the variables, and some may even effectively disappear in the machine code in higher optimizations. The compiler will then decide how much space is needed by the variables, and then later, during execution, create a space known as the stack where the variables live.

    When a function is called, all of the variables that are used by your function are put on the stack, along with information about the function that is called (i.e. the return address, parameters, etc.). It doesn't matter where the variable was declared, just that it was declared - and it will be allocated onto the stack, regardless.

    Declaring variables isn't "expensive," per se; if it's easy enough to be not used as a variable, the compiler will probably remove it as a variable.

    Check this out:

    Da stack

    Wikipedia on call stacks, Some other place on the stack

    Of course, all of this is implementation-dependent and system-dependent.

    0 讨论(0)
  • 2021-02-01 00:45

    The best practice is to adapt a lazy approach, i.e., declare them only when you really need them ;) (and not before). It results in the following benefit:

    Code is more readable if those variables are declared as near to the place of usage as possible.

    0 讨论(0)
  • 2021-02-01 00:49

    In C99 and later (or with the common conforming extension to C89), you are free to mix statements and declarations.

    Just as in earlier versions (only more so as compilers got smarter and more aggressive), the compiler decides how to allocate registers and stack, or do any number of other optimizations conforming to the as-if-rule.
    That means performance-wise, there's no expectation of any difference.

    Anyway, that was not the reason such was allowed:

    It was for restricting scope, and thus reducing the context a human must keep in mind when interpreting and verifying your code.

    0 讨论(0)
  • 2021-02-01 00:49

    If you have this

    int function ()
    {
       {
           sometype foo;
           bool somecondition;
           /* do something with foo and compute somecondition */
           if (!somecondition) return false;
       }
       internalStructure  *str1;
       internalStructure *str2;
       char *dataPointer;
       float xyz;
    
       /* do something here with the above local variables */    
    }
    

    then the stack space reserved for foo and somecondition can be obviously reused for str1etc., so by declaring after the if, you may save stack space. Depending on the optimization capabilities of the compiler, the saving of stack space may also take place if you flatten the fucntion by removing the inner pair of braces or if you do declare str1 etc. before the if; however, this requires the compiler/optimizer to notice that the scopes do not "really" overlap. By positining the declarations after the if you facilitate this behaviour even without optimization - not to mention the improved code readability.

    0 讨论(0)
  • 2021-02-01 00:50

    Yes, it can cost clarity. If there is a case where the function must do nothing at all on some condition, (as when finding the global false, in your case), then placing the check at the top, where you show it above, is surely easier to understand - something that is essential while debugging and/or documenting.

    0 讨论(0)
  • 2021-02-01 00:55

    Keep the declaration as close to where it's used as possible. Ideally inside nested blocks. So in this case it would make no sense to declare the variables above the if statement.

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