Beginner beware: Use of strong and weak symbols in C language _c language

Source: Internet
Author: User
Statement: The following examples are all under Linux to try, window did not try. Those who are interested can try. Article needle C beginner.
C language strong symbol and weak symbol is C beginners often easy to make mistakes in the place. And many times, especially with the development of many people with the program, it causes problems are often very strange and difficult to locate.
What are strong and weak symbols?
In the C language, functions and initialized global variables are strong symbols, and uninitialized global variables are weakly symbolic. The definition of strong and weak symbols is the connector used 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 symbol;
If more than one weak symbol, select any.
It's a trap:
Code on:
Copy Code code as follows:

Main.c
#include <stdio.h>
int fun ();
int x;
int main ()
{
printf ("In main.c:x=%p\n", &x);
Fun ();
return 0;
}
TEST.c
#include <stdio.h>
int x;
int fun ()
{
printf ("In test.c:x=%p\n", &x);
return 0;
}

Compiling: gcc main.c test.c, running, results:
In Main.c:x=0x80496a8
In Test.c:x=0x80496a8
Two x is a variable. This may be said in the past, perhaps a forgotten plus extern.
Look again:
Copy Code code as follows:

Main.c
#include <stdio.h>
int fun ();
int x;
int main ()
{
printf ("In main.c:&x=%p\n", &x);
Fun ();
return 0;
}

Copy Code code as follows:

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>

Copy Code code as follows:

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

Copy Code code as follows:

} x;
int fun ()
{
printf ("In test.c:&x=%p\n", &x);
return 0;
}

Run Result:
In main.c:&x=0x80496e0
In TEST.C:&X=0X80496E0
Connectors also consider them to be a variable, a time when programmers are very likely to think that they are two variables (or good programmers). On the contrary, the same memory, in different files will have different types and meanings. These two files in the memory read and write process, will affect each other, causing very strange problems.
Imagine if it was a program that was developed by multiple people, and if they had only one global variable with the same name and did not initialize, then a problem would arise.
It is good to have problems in a program, after all, the code is together. What happens if you use a dynamic library or a static library that has uninitialized global variables and exactly the same name you defined? I have tried, as above, that the two variable addresses of the conflict are the same. And this time if you do not have the source of the library, when there is a problem, the variable is modified, you estimate to go a lot of detours to think is the library changed your variable. This is a problem I have solved. Since then, I have requested that the source of all libraries in our company not have a non static global variable.
How to avoid it?
1, the best policy: to find ways to eliminate global variables. Global variables will increase the coupling of the program to which he wants to control the use. If you can replace the best in other ways.
2, China Policy: There is no way, that the global variable is defined as static, it is not strong or weak points. And does not conflict with other global symbols. As for other files that might be accessible to him, they can be encapsulated into functions. It is a good practice to encapsulate the data of a module.
3, the worst thing: all the symbols into a strong symbol. All the global variables are initialized, remember, all of them. If one is not initialized, it may conflict with others, although others are initialized. (Write your own code to test it).
4, the necessary strategy: GCC provides an option to check this type of error:-fno-common.
Why does C language design it?
Easy to cause problems, what's going on with a feature of C? May be the cause of history, not to delve into. But I think it may also be the reason for part of the philosophy of language design: The philosophy of C language design is to fully believe in programmers, giving them the greatest power and flexibility. This feature may be useful in certain special situations.
the gentleman and villain in the language:
The ancients said to be near the gentleman, far villain. Like today said this characteristic (community also can calculate a), should be C language "villain" (Pat, may say heavier). We are better off at a respectful distance. Kangxi seems to have said, (special time) not only to govern the country with a gentleman, but also to use the villain, but to be able to master properly. Otherwise it will backfire.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.