Caution: strong and weak symbols in C Language

Source: Internet
Author: User

Declaration: The following instances are all attempted in Linux, but not in windows. If you are interested, try it. Article needle C for beginners.

Strong and weak symbols in C language are common mistakes for C beginners. In addition, in many cases, especially programs developed by many people, the problems it causes are often weird and difficult to locate.

What are strong and weak symbols?

In C language, functions and initialized global variables are strong symbols, and they are weak symbols when uninitialized global variables are used. The definition of strong and weak symbols is used by connectors to process multiple definition symbols. The rule is as follows:

Multiple strong symbols are not allowed;

If a strong symbol and a weak symbol are used, select a strong symbol;

If multiple weak symbols exist, select any one.

Its traps:

Code:

//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;}

Compile: GCC main. C test. C. Run. Result:
In Main. C: x = 0x80496a8
In test. C: x = 0x80496a8

Two 'X' are a variable. In the past, we may have forgotten to add extern.

Let's look at it again:

//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>struct{char a;char b;char c;char d;
int t;
} x;int fun(){printf("in test.c:&x=%p\n", &x);return 0;}

Running result:

In Main. C: & X = 0x80496e0
In test. C: & X = 0x80496e0

The connector also thinks that they are a variable. At this time, programmers are very likely to think that they are two variables (or good programmers will ). On the contrary, the same memory has different types and meanings in different files. These two files will affect each other in the process of reading and writing this memory, causing a very strange problem.

Imagine that if a program is developed by multiple developers at the same time, if they only have one global variable with a duplicate name and are not initialized, the problem will arise.

It is good to have problems in a program. After all, the code is all together. If the dynamic or static library you are using has uninitialized global variables and exactly the same name as your definition, what is the result? I tried. Like above, the addresses of the two conflicting variables are the same. At this time, if you do not have the source code of the database, when a problem occurs and the variable is modified, you may have to take a lot of detours to think that the database has changed your variable. This is a problem I have solved. Since then, I have asked that non-static global variables cannot appear in the source code of all the libraries of our company.

How to avoid it?
1. Best strategy: Find a way to eliminate global variables. Global variables increase program coupling and control the use of them. If you can use other methods instead of the best one.

2. zhongce: there is no way to define a global variable as static. It has no strength or weakness. It does not conflict with other global symbols. Access to other files may be encapsulated into functions. It is a good practice to encapsulate the data of a module.

3. Strategy: convert all the symbols into strong ones. All global variables are initialized. Remember, they are all. If one is not initialized, it may conflict with others, even though others initialize. (Write your own code to test ).

4. Required strategy: GCC provides an option to check for such errors:-fno-Common.

Why is C language designed?

It is easy to cause problems. What is a feature of C? It may be the reason of history. But I think it may also be the reason for some language design philosophy: One thing about the C language design philosophy is to fully trust programmers and give them the greatest rights and flexibility. This feature may play a role in some special circumstances.

The gentleman and villain in the language:
The ancients said they would like to be near a gentleman, a little man. This feature, as we said today (the community can also be regarded as one), should be a "villain" in the C language (Pat, it may be said heavy ). We are better off. Kangxi seems to have said that (in special periods) governing the country should not only use a gentleman, but also use a villain, but be able to properly control the country. Otherwise, it will lead you.

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.