Can I get best performance making static variables?

前端 未结 5 391
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-01 07:16

Why some people do:

char baa(int x) {
    static char foo[] = \" .. \";
    return foo[x ..];
}

instead of:

char baa(int x) {
          


        
5条回答
  •  猫巷女王i
    2021-02-01 08:16

    Yes it makes difference , if u have declared a variable as static :

    1. Firstly, the memory will be allocated in either bss or data segment instead of stack.

    2. Secondly, it will be initialized for once only , not every time unlike other variables of function, which will surely create difference.

    3. Thirdly, it retains it's value b/w function calls.So depending on the situations you should use it.

提交回复
热议问题