Why global variables must be initialized?

First, the initialization rules section

Before explaining why initialization, first mentioned initialization rules of the C language, for later use.
We may use in the array is initialized this way:

char buf[10] = {0};

Then  char buf [ 10 ] = { 1 }; are not each array each element is initialized to 1 out?

In actual fact, according to the characteristics of the compiler, when specifying initialization element, if the number of elements less than the total number of array elements, then the other elements will be initialized to zero.

We can use this feature to verify a piece of code:

1 #include <stdio.h>
2 
3 int main()
4 {
5   char buf[10] = {1};
6 
7   return 0;
8 }

Disassembly thereof as follows:

 1 <main>:
 2   push {fp} ; (str fp, [sp, #-4]!)
 3   add fp, sp, #0
 4   sub sp, sp, #20
 5   sub r3, fp, #16
 6   mov r2, #0
 7   str r2, [r3]
 8   str r2, [r3, #4]
 9   strh r2, [r3, #8]
10   mov r3, #1
11   strb r3, [fp, #-16]
12   mov r3, #0
13   mov r0, r3
14   add sp, fp, #0
15   pop {fp} ; (ldr fp, [sp], #4)
16   bx lr

A compiled code portion rows 6-11 wherein the clear, except that the first byte of the program assigned to the array 1, and the remaining bytes are assigned to zero.

 

Second, the global variable initialization section

Said the following about how we create global variables to be initialized, if you want to create a variable initial value of 0, then the "= 0" assignment Can omit it?

Charges did not talk much, use the code to explain everything.

If the following scenario, a company for the realization of a project, A module_a.c programmers write the program, his colleagues wrote module_b.c B program, the final two people to write source code as follows:

 1 /* module_a.c */
 2 #include <stdio.h>
 3 
 4 void function(void);
 5 
 6 int global = 0;
 7 
 8 int main()
 9 {
10     global = 3;
11     function();
12     printf("main: %d \n", global);
13     return 0;
14 }
 1 /* module_b.c */
 2 #include <stdio.h>
 3 
 4 int global;
 5 
 6 void function(void)
 7 {
 8     global = 6;
 9     printf("function: %d \n", global);
10     return 0;
11 }

Two people have consonance definition of the same global variables global, but the B an oversight, forgot to global initialized.

Compiled by normal, and the results are as follows:

function: 6
main: 6

We can see, my colleagues A compilation of modules produced operating results inexplicable. Why define global variables with the same name, the compiler does not complain of it? We continue to explore.

First, the source files are compiled into object files, then use the tool to view the contents of readelf.

arm-linux-gcc -c module_x.c
arm-linux-readelf -a module_x.o

Only the removal of the relevant contents of which  global identifier a module are as follows:

    ...
[4] .bss    NOBITS    00000000 000078 000004 00 WA 0 0 4
    ...
13: 00000000    4 OBJECT GLOBAL DEFAULT    4    global
    ...

b global identification module is as follows:

    ...
12: 00000004    4 OBJECT GLOBAL DEFAULT COM global
    ...

Whereby that, the global variable is initialized to 0. The combined to .bss section, uninitialized global variables incorporated into the COM (common block) segments. The reason is consistent with the default behavior of gcc compiler and traditional unix c compiler, uninitialized global variables placed in the common block,  common block is equivalent to weak symbols (weak symbol), so when the link does not complain, this may be a very hard to identify the BUG.

 

The following summary found on the Web:

  The weak global symbol of the same name and symbolic links can not go wrong, the linker will choose the global symbol. Similarly, if there is a global symbol and a number of the same name in common block in, the linker will select global symbol. In other words, the link is considered uninitialized global variables is weak symbol.

Of course, the way to avoid this happening is there, we can add -fno-common attribute to turn off this feature gcc at compile time, if there is a global variable of the same name, it will generate an error link, user awareness problem lies in.

Guess you like

Origin www.cnblogs.com/GyForever1004/p/11448828.html