1

If I have a function that is called A LOT of times, and that function needs an array of 16 pointers that will be updated with new pointers every time it's called, is this the right way to declare this array?

char** readUserInput() {
    static char* cmds[16];
    ...

}

Will this array be initialized once?

Benjy Kessler
  • 7,356
  • 6
  • 41
  • 69
johni
  • 5,342
  • 6
  • 42
  • 70
  • As long as you don't initialize the array **at declaration**, it doesn't really make any difference in terms of runtime performance. If the array is static, then it is allocated in the data-section. Otherwise, it is allocated in the stack. But in either case, the allocation (a.k.a "static allocation", but don't confuse it with the keyword `static` here) does not add any executable code, hence does not yield any runtime impact. – barak manos Apr 25 '15 at 20:44
  • If you do initialize the array at declaration **AND** it is non-static, then an initialization code is implicitly added to the function, and it will be executed every time the function is called. That **will** affect runtime performance of course. – barak manos Apr 25 '15 at 20:44
  • Please note that the allocation details that I've mentioned above (data-section vs stack) are not dictated by the C-language standard, but are dependent on compiler implementation (though every compiler that I've ever heard of implements it in the same manner). – barak manos Apr 25 '15 at 20:48

4 Answers4

4

Yes, static variables are only initialized once. By declaring the variable cmds you are declaring an array of 16 char*s. The array is initialized with zeroes. The array will never be initialized again.

Take this code as an example:

int i;
static char *cmds[5];
for (i = 0;i<5;++i) {
  printf("%d ", cmds[i]);
}

It prints:

0 0 0 0 0

Benjy Kessler
  • 7,356
  • 6
  • 41
  • 69
  • I think you should point out that in c declaration `!=` initialization/definition. – Iharob Al Asimi Apr 25 '15 at 20:01
  • @BenjyKessler: By that, you have made this answer obsolete and irrelevant. In addition, your edit is simply wrong and misses OP's intentions, since the array is **not initialized** in the given code snippet. OP used the word "declared" because that is what he or she were doing - only declaring, not initializing. This answer explains what's wrong with OP's understanding of the word "declaration", so by editting the question you have essentially ruined your own answer. I think you should revert your changes. – barak manos Apr 25 '15 at 20:51
  • 1
    http://stackoverflow.com/questions/2091499/why-global-and-static-variables-are-initialized-to-their-default-values – Benjy Kessler Apr 25 '15 at 20:52
3

It is declared only once regardless of whether it houses the static storage specifier. Don't confuse declare with lifetime.

If the real question is "will there be only one instance of cmds, and will its content persist between calls?", then yes. It is declared with the static storage class specifier. Per §6.2.4.3 of the C11 standard

... Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup."

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
2

Static variables are only initialized/declared once and the static keyword is useful to provide a lifetime over the entire program, but limit their scope.

codingEnthusiast
  • 3,800
  • 2
  • 25
  • 37
2

It is unclear whether you have to return that array from hot function. If yes, it has to be static or has to be passed as an argument. Even if not, then there is no difference in "speed" between static and auto-array, because your reuse scheme should anyway have means of [possibly no-op] preinitialization before call, and no matter what very first initial value was.

Drawback of static storage is that code becomes non-reentrant. Passing it as an argument would be more correct solution.

user3125367
  • 2,920
  • 1
  • 17
  • 17