Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About "static" in C, how is it implemented by the compiler?

Tags:

c

About "static" in C, how is it implemented by the compiler ?

This is a Bloomberg interview question. Any thoughts ?

like image 894
Josh Morrison Avatar asked Feb 09 '11 20:02

Josh Morrison


1 Answers

[[I'm assuming we're talking about static in the context of variables here, because static functions are simply a compile/link-time thing, with no run-time implications.]]

In short, it's implementation-specific. The compiler is free to do anything it chooses.

Typically (but by no means exclusively), statics are stored in the .bss or .data sections of the executable image at fixed locations. This has performance advantages, as they can be accessed with literal addresses, rather than pointer dereferences (as would be the case for stack-based variables). As this is part of the binary, this also means that the initial values are automatically mapped into memory when the executable is first loaded; no intialisation routines are required.

like image 120
Oliver Charlesworth Avatar answered Sep 23 '22 00:09

Oliver Charlesworth