1

In C, global variables are stored with the program code. I.e. the space to hold them is part of the object file (either in the data or bss section), instead of being allocated during execution (to either the stack or heap).

What other ways are used by languages to store global variables? For example, consider a dynamic language like Python. What about recursive languages like Lisp? Does class-orientation come into play? Do "new" languages (with the benefit of hindsight) take a different approach?

Is there any substantial benefit that C gains by storing global variables in-situ with the code?

  • One thing to consider is languages with module systems. – Telastyn Oct 10 '19 at 21:28
4

No, the global variables are not necessarily stored with the code.

The C standard defines the global variables to be of static storage duration (caution: static storage duration is not to be confused with static variables, which are a subset thereof). It does not guarantee that the global variables are preallocated in the executable file, but only that:

6.2.4/3: (...) Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.

A compliant C implementation could therefore perfectly well allocate and initialize global variable at runtime, as long as this is done before the programme startup.

The programme startup, by the way, is not the start of the executable process by the operating system. The C standard explains here what it is:

5.1.2/1: (...) program startup occurs when a designated C function is called by the execution environment. All objects with static storage duration shall be initialized (set to their initial values) before program startup. The manner and timing of such initialization are otherwise unspecified.

It is however true that many C language implementations behave like you explain. But this is not specific to C. It is related to the use of a feature offered in many operating systems, which allow to define data segments in the executable file to be loaded with pre-initialized data, the adress of which being resolved by the linker when the executable file is generated (and eventually relocated by the loader).

| improve this answer | |
  • "It is related to the use of a feature offered in many operating systems, which allow to define data segments in the executable file..." - Does this mean most (maybe all) "static" language compiler-linkers make use of the data segment when generating the final exe/object? (I.e. as opposed to "dynamic" languages where there is no ultimate exe loaded by the OS for execution). What benefit is there to using the data segment instead of runtime allocated stack/heap memory? Ultimately when reading the value (whether in code/stack/heap) it is still access to the same physical memory chip... – Jet Blue Oct 11 '19 at 0:19
  • "It does not guarantee that the global variables are preallocated in the executable file, but only that..." - Ah I see, thank you for clarifying this. Do you know of examples where this isn't the case? I assume GCC is why this is the standard approach...Or is it x86 assembly's use of the .bss and .data sections?... – Jet Blue Oct 11 '19 at 0:35
  • @JetBlue data segments in assembly are more related to loaders thant CPU. Relocatable segments already existed in MS-DOS and perhaps even before. The alternative would be to initialise constants at runtime: this would require a lot of extra code to store constant values at the right place. Using heap for globals would in addition require to use pointers instead of direct addressing. Not a probleme, but C's philolosphy is to avoid overhead when it's not absolutely necessary. – Christophe Oct 11 '19 at 6:36
  • @JetBlue Finally, there are languages that may use a mixed approach when required: in C++ you can have global objects that require a constructor to be invoked at runtime (and maybe allocate some stuff in the free store). This is then done before the startup of the programme (i.e. before main is called). – Christophe Oct 11 '19 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.