11

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)?

dbush
  • 205,898
  • 23
  • 218
  • 273
nebuch
  • 6,475
  • 4
  • 20
  • 39
  • 1
    IIRC `static` variables are placed in a different section. See also here: http://stackoverflow.com/questions/93039/where-are-static-variables-stored-in-c-c – Bart Friederichs Jul 19 '16 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)? – mvidelgauz Jul 19 '16 at 15:30
  • 3
    `static` means the variable is not allocated in the stack (stored in data segment or in BSS segment). – Rotem Jul 19 '16 at 15:30
  • 1
    Try allocating large array without static: `int arr[100000000];`, and see what happens. – Rotem Jul 19 '16 at 15:32
  • @mvidelgauz Pretty sure the standard specifically says you cannot call `main`. – user975989 Jul 19 '16 at 15:32
  • @user975989 I never had an idea to try it but now I am curious... Any links? – mvidelgauz Jul 19 '16 at 15:34
  • 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. – too honest for this site Jul 19 '16 at 15:42
  • @Olaf thank you for comment. although question is tagged with `c` I am curious to see c++ related link(s) also, do you have any? – mvidelgauz Jul 19 '16 at 15:48
  • @mvidelgauz I was mistaken, was thinking of C++ and thought it applied the same. In C++ it's `6.3.1.3` which states `The function main shall not be used within a program`. – user975989 Jul 19 '16 at 15:51
  • @user975989 JFTR VS2013 didn't emit even warning for recursive call of `int _tmain(int argc, _TCHAR* argv[])` (in C++). Is standard talking about function, which is main application entry point or about function that is literally named _"main"_ (and not "_tmain" or other variations)? – mvidelgauz Jul 19 '16 at 15:59
  • @mvidelgauz: `_tmain` is not standard. The standard apparently only covers standard behaviour. If your code does not have a `main`, it is not compliant to the standard. And the question is about C, not C++. Please don't argue with different languages! – too honest for this site Jul 19 '16 at 16:07
  • @Olaf I didn't argue, I only asked questions and that comment was addressed to user975989 following his comment – mvidelgauz Jul 19 '16 at 16:16
  • @mvidelgauz: If you have a question, please first do research on your own (the final drafts of the standards are freely available). As a last resort ask a question. I'm sure this has already been covered here. – too honest for this site Jul 19 '16 at 16:18

2 Answers2

10

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.

dbush
  • 205,898
  • 23
  • 218
  • 273
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.

cleblanc
  • 3,678
  • 1
  • 13
  • 16
  • 5
    `.bss` is only for default initialized data (zeros only). Non-zero globals go to `.data`. – Sergio Jul 19 '16 at 15:30
  • @nebuch: which part of this paragraph do you speak about ? – blatinox Jul 19 '16 at 16:54
  • 1
    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. – Eugene Sh. Jul 19 '16 at 17:15
  • @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 Jul 20 '16 at 03:29
  • @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". – blatinox Jul 20 '16 at 08:51
  • @cleblanc: could you explain why do you think it goes to `.bss` section ? – blatinox Jul 20 '16 at 08:52
  • It goes in .data not .bss my mistake. – cleblanc Jul 20 '16 at 13:02
  • Couldn't a large array overflow the data section too? Is data larger than the stack? – jinawee Aug 08 '18 at 16:09