Can I get best performance making static variables?

前端 未结 5 453
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  Happy的楠姐
    2021-02-01 08:14

    Yes, the performance is different: unlike variables in the automatic storage that are initialized every time, static variables are initialized only once, the first time you go through the function. If foo is not written to, there is no other differences. If it is written to, the changes to static variables survive between calls, while changes to automatic variables get lost the next time through the function.

提交回复
热议问题