Novice caution: the use of strong and weak symbols in c

  • 2020-04-01 23:37:57
  • OfStack

Declaration: all the following instances were tried under Linux, not under window. Those who are interested can have a try. Article needle c beginners.
The strong and weak symbols of c are a common mistake for c beginners. And a lot of times, especially with multi-person programs, it can cause problems that are very behavioral and difficult to locate.
What are strong and weak signs?
In c, functions and initialized global variables are strongly signed, while uninitialized global variables are weakly signed. The definition of strong and weak symbols is used by the connector to handle multiple definition symbols, and its rules are:
Multiple strong symbols are not allowed;
If a strong sign and a weak sign, this selects the strong sign;
If there are more than one weak symbol, choose any one.
Its pitfalls:
The code:

//main.c
#include <stdio.h>
int fun();
int x;
int main()
{
 printf("in main.c:x=%pn", &x);
 fun();
 return 0;
}
//test.c
#include <stdio.h>
int x;
int fun()
{
 printf("in test.c:x=%pn", &x);
 return 0;
}

GCC main.c test.c, run, result:
In the main. C: x = 0 x80496a8
In the test. C: x = 0 x80496a8
Two x's are a variable. This might be said in the past, maybe one forgot to add extern.
Then look at:

//main.c
#include <stdio.h>
int fun();
int x;
int main()
{
 printf("in main.c:&x=%pn", &x);
 fun();
 return 0;
}


//test.c
#include <stdio.h>
struct
{
<span style="white-space:pre"> </span>char a;
<span style="white-space:pre"> </span>char b;
<span style="white-space:pre"> </span>char c;
<span style="white-space:pre"> </span>char d;<span style="white-space:pre"> </span>


<span style="white-space:pre"> </span>int t;


} x;
int fun()
{
 printf("in test.c:&x=%pn", &x);
 return 0;
}

Operation results:
In the main. C: & x80496e0 x = 0
In the test. C: & x80496e0 x = 0
Connectors also think of them as one variable, at which point programmers are more likely to think of them as two variables (or good programmers will). On the contrary, the same block of memory has different types and meanings in different files. These two files affect each other as they read and write to this memory, causing a very strange problem.
Imagine if a program was developed by multiple people at the same time, and if they had only one global variable with the same name and no initialization, that would cause problems.
It's ok to have a problem in a program because the code is all in one place. What happens if you use a dynamic or static library that has an uninitialized global variable that happens to have the same name as the one you defined? I've tried that, and again, the conflicting variable addresses are the same. And at this time if you do not have the source of the library, when there is a problem, the variable is modified, you are expected to take a lot of detours to think that the library changed your variable. This is a problem that I have solved. Since then, I've required that no non-static global variables appear in the source code of any of our libraries.
How to avoid it?
1. The best policy: find a way to eliminate global variables. Global variables increase the program's coupling and control its use. If you can use other methods instead of the best.
2, the best policy: there is no way, then the global variable is defined as static, it is not strong or weak. It will not conflict with other global symbols. As for possible access to it from other files, it can be encapsulated as a function. Encapsulating a module's data is a good practice.
3, the bottom line: all the signs are all strong signs. All global variables are initialized, remember, all of them. If one is not initialized, it may conflict with others, even if they are initialized. Write your own code and test it out.
GCC provides an option to check for this type of error: -fno-common.
Why did c design it?
It causes problems. What's going on? A property of C? It may be because of history. But I think it may also be part of the philosophy of language design: one of the things about the design philosophy of c is that it trusts programmers enough to give them the most power and flexibility. This feature may come into play in some special circumstances.
The gentleman and the villain in language:
The ancients say to want near gentleman, far villain. As mentioned today, this feature (community can also be counted as one) should be the "little man" in c language. We'd better keep our distance. Kangxi seems to have said that the government should not only use the gentleman, but also use the villain, but also be able to manage properly. Or you'll get burned.

Related articles: