Well, let's change the previous helloworld program source code to following:
We newly added the SECOND line to declare a new static variable named s_Age, and set the initialized value with 0xAE; why to use the value of 0xAE? The most important reason is that it is the certain value and we can track it with this value in the following steps.
Ok, let's compile it with
$> g++ -o helloworld helloworld.cpp -save-temps
So, check the intermediate file "helloworld.ii", see what changed.
$> vim helloworld.ii
The new file contents what we concern are as following:
1136 # 2 "helloworld.cpp" 2
1137 static int s_Age = 0xAE;
1138 int main(int argc, char ** argv)
1139 {
1140 printf("Hello World/r/n");
1141 return 0;
1142 }
There are NO any changes for this change. Let's take a look at the Intermediate Assembly File.
So, we can see the newly added static variable s_Age in the line 4 to line 5. It decorates the variable name with prefix "_", the new variable name for s_Age is "_s_Age" (in deed, it's a label for variable s_Age) , and it's value is the DEC value "174" of hex number "0xAE".
And look at the line 2 and line 3, these TWO lines declares the DATA secion, and the label _s_Age is just living in the DATA secion. Obviously, this DATA section can be accessed with READ and WRITE.
Well, make a conclusion, it is "STATIC variables are living in the DATA secion ". Is it really TRUE? Follow me, let's continue.
We will limited the variable s_Age with "const" keyword. Modify the source code for the declaration of variable s_Age with following code:
Compile the source code with option "-save-temps" and see the assembly file "helloworld.s". Now, it is contains following contents:
Can you see the last 5 line in the bottom? The const static variable s_Age is living in the READ-ONLY data section.
So, the Words Of Wisdom is:
- The global static constant variables are placed in the READ-ONLY data section.
- The global static non-constant variables are placed in the regular data secion.
Are above conclusions correct? Let's practice to take a check. Modify the source code for the declaration of s_Age with following:
Compile the source code and check the assembly file, it should be as following: