Is declaration of variables expensive?

前端 未结 12 971
长情又很酷
长情又很酷 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 01:02

    It ultimately depends on the compiler but usually all locals are allocated at the beginning of the function.

    However, the cost of allocating local variables is very small as they are put on the stack (or are put in a register after optimization).

提交回复
热议问题