13

Reading over someone else's code, I saw something syntactically similar to this:

int main(void) { static int attr[] = {FOO, BAR, BAZ, 0}; /* ... */ } 

Is this an error or is there some reason to declare a variable in main static? As I understand it static prevents linkage and maintains value between invocations. Because here it's inside a function it only does the latter, but main is only invoked once so I don't see the point. Does this modify some compilation behavior (e.g. preventing it from being optimized out of existence)?

13
  • 1
    IIRC static variables are placed in a different section. See also here: stackoverflow.com/questions/93039/… Commented Jul 19, 2016 at 15:28
  • although this is probably bad practice but, formally, who prevents you (or someone else) from calling main recursively (later in the code)? Commented Jul 19, 2016 at 15:30
  • 3
    static means the variable is not allocated in the stack (stored in data segment or in BSS segment). Commented Jul 19, 2016 at 15:30
  • 1
    Try allocating large array without static: int arr[100000000];, and see what happens. Commented Jul 19, 2016 at 15:32
  • 3
    @user975989: Please state the section in the standard! IIRC it is exactly the opposite for C (which does not mean it is recommended practice). C++ does not allow calling main recursively, but that is a different language. Commented Jul 19, 2016 at 15:42

2 Answers 2

12

Unless you're doing something very non-standard such as calling main directly, there's little point in declaring local variables static in main.

What it is useful for however is if you have some large structure used in main that would be too big for the stack. Then, declaring the variable as static means it lives in the data segment.

Being static also means that, if uninitialized, the variable will be initialized with all 0's, just like globals.

Sign up to request clarification or add additional context in comments.

1 Comment

Good answer, but your second two points are precisely why there can be every reason to do it (especially on, for example, an embedded platform)
6

static also tells the compiler to store the data in .data section of memory where globals are typically stored. You can use this for large arrays that might overflow the stack.

8 Comments

.bss is only for default initialized data (zeros only). Non-zero globals go to .data.
@nebuch: which part of this paragraph do you speak about ?
It is definitely not going to BSS, as it has an explicit initializer (unless it is all zeros). You better correct the answer, as it is correct otherwise.
@blatinox: ``static local constants must be initialized at declaration, however, as they do not have a separate declaration, and thus are typically not in the BSS section''
@nebuch: we do not speak about constants here. The same link says: "Hence, the BSS segment typically includes all uninitialized objects (both variables and constants) declared at file scope (i.e., outside any function) as well as uninitialized static local variables".
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.