Let say I have an header file Resources.h
where I have defined these 5 structs:
const IColor COLOR_BLACK(255, 0, 0, 0);
const IColor COLOR_GRAY(255, 127, 127, 127);
const IColor COLOR_WHITE(255, 255, 255, 255);
const IColor COLOR_RED(255, 255, 0, 0);
const IColor COLOR_GREEN(255, 0, 255, 0);
Using const
(static
by default in C++
, so internal linkage
) they "reside" in the scope of a translation unit.
Now, let say I include this files 10 times into my application (from 10 different .cpp
).
When I compile, an object file is created, and (later) the Linker will gather all these object files together into a unique runnable code for the machine.
Does this means that when I run
the program, it will allocate in memory 10 times each structs above? i.e. 10x5 structs?
So they are separate for translation unit even when they are linked together later? Or linker is smart enough to converge them to a unique allocation in memory?
Not sure if I got these steps corerctly. I'm fancy new in C++.