1

How does different translation units represented in memory?

For e.g.

//InternalLinkage.h
static int var = 10;

//File A.h
void UpdateStaticA();

//File A.cpp
#include "InternalLinkage.h"
void UpdateStaticA()
{
  var = 20;
}

//File B.h
void UpdateStaticB();

//File B.cpp
#include "InternalLinkage.h"
void UpdateStaticB()
{
    var = 30;
}

//main.cpp
#include "InternalLinkage.h"
#include "ClassA.h"
#include "ClassB.h"
int _tmain(int argc, _TCHAR* argv[])
{
    UpdateStaticA();
    UpdateStaticB();
    printf("var = %d",var);
}

The output here is 10 as we know we have three translations units each having there own copy of static variable var. So now the question is

  1. How are these translation units maintained in memory?
  2. Do we have separated sections for each translation unit in memory so that compiler can maintain static variable 'Var' for each translation unit?

Clearly there are three different copies of variable 'var' in memory so how does compiler maintain which copy belongs to which translation unit?

anonymous
  • 37
  • 2
  • What do you mean by "translation unit"? There's only one variable "var". Since it is global initialised data the compiler will put that into the data section. Not sure exactly what you are asking. – kaylum Apr 09 '15 at 23:36
  • By Translation unit I mean a file(.c/.cpp) after including all the header files – anonymous Apr 09 '15 at 23:44
  • In the example shown above A.cpp, B.cpp and main.cpp can be treated as three different translation units and since they all include 'InternalLinkage.h' they all have a copy of static variable 'var' which has a scope of the translation unit in which it is defined. – anonymous Apr 09 '15 at 23:46
  • Each translation unit will be compiled into a seperate object file each with different symbol tables. In each symbol table there will be an entry for "var". When the objects are linked together a single symbol table will be created in the final executable with two different "var" entries. – kaylum Apr 09 '15 at 23:54
  • Two or Three copies? I believe there should three entries. – anonymous Apr 10 '15 at 00:13
  • When the final executable is loaded in memory static data goes in static data section, then how does the distinction is made that which "var" belongs to which translation unit? – anonymous Apr 10 '15 at 00:15
  • Sorry, yes three. I was speaking in general and wasn't specifically describing your example. The answer is the same - via the symbol table. Basically the compiler generates code that doesn't reference variables by absolute addresses but via symbol table entries. During linking the symbol table is used to resolve those references. So each reference of "var" will look up the relevant (different) symbol table entry which will be resolved at link time. Have a look at this post for example for some more detials: http://stackoverflow.com/questions/69112/what-is-a-symbol-table – kaylum Apr 10 '15 at 00:29
  • the compiler only compiles one file at a time. It is the linker that needs to keep the three instances of the var variable allocated to three separate places in memory – user3629249 Apr 11 '15 at 12:01
  • because of the 'static' modifier, there will be a separate 'var' in each file that includes the internallinkage.h file – user3629249 Apr 11 '15 at 12:03

0 Answers0