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
- How are these translation units maintained in memory?
- 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?