How is static variable initialization implemented by the compiler?

前端 未结 5 2033
旧巷少年郎
旧巷少年郎 2020-12-02 11:50

I\'m curious about the underlying implementation of static variables within a function.

If I declare a static variable of a fundamental type (char, int, double, etc.

5条回答
  •  独厮守ぢ
    2020-12-02 12:09

    Another twist is in embedded code, where the run-before-main() code (cinit/whatever) may copy pre-initialized data (both statics and non-statics) into ram from a const data segment, perhaps residing in ROM. This is useful where the code may not be running from some sort of backing store (disk) where it can be re-loaded from. Again, this doesn't violate the requirements of the language, since this is done before main().

    Slight tangent: While I've not seen it done much (outside of Emacs), a program or compiler could basically run your code in a process and instantiate/initialize objects, then freeze and dump the process. Emacs does something similar to this to load up large amounts of elisp (i.e. chew on it), then dump the running state as the working executable, to avoid the cost of parsing on each invocation.

提交回复
热议问题