Reputation: 7636
About "static" in C, how is it implemented by the compiler ?
This is a Bloomberg interview question. Any thoughts ?
Upvotes: 5
Views: 1418
Reputation: 106096
static global variables will generally be allocated a fixed address at compile time. Many OS provide memory regions that are 0-initialised, others that map part of the executable image with preinitialised data, as well as totally uninitialised areas. Depending on whether the compiler can work out the correct initial content of the static variable at compile time, it may select the most appropriate of these memory regions, calling any run-time initialisation later if required. For example:
static int x; // needs to be 0 before main() runs
// best way: 0-initialised memory area
static int y = 3; // best way: map/copy area of executable already containing "3"
static int z = time(NULL); // initial value unimportant
// best way: uninitialised memory area
// pre-main() init code
Notes: putting e.g. z
in 0-initialised memory then clobbering it is not significantly wasteful - just not strictly necessary. Some OS may have separate areas for read-only/const values.
static local variables with compile-time known initial values may be created as per globals. For run-time initialised values (only legal in C++) compilers tend to (must?) initialise them when the scope is first entered. This is typically coordinated by having an implicit supporting boolean value per scope containing static local variables: each time the scope is entered the boolean is consulted to see whether the statics need to be initialised. On some compilers, this may actually be done in a less efficient but thread-safe fashion, such that static locals can be used for singleton instances.
EDIT: Given Lundin's comment (correctly) asserting all C statics must be initialised before main()
, I wrote some code to explore this:
#include <stdio.h>
#include <time.h>
void f()
{
static int i = time(NULL);
printf("%d\n", i);
}
int main()
{
int i = time(NULL);
printf("%d\n", i);
sleep(2);
f();
}
With GCC's C compiler, I get a fatal compilation error about the local static i
requiring initialisation at run-time. Compiled as C++ (my main language), this is perfectly legal and initialised at run-time and after entering main()
- showing that that part of my explanation above is only relevant to C++.
static functions are simply marked in the generated object such that the linker won't consider them when matching unresolved calls from other objects.
Upvotes: 2
Reputation: 272497
[[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.
Upvotes: 3