Some of the ugly views about strong, weak, strong, and weak references in C language, please correct me.

Source: Internet
Author: User

First I said it was tragic that I didn't know that C had strong symbols, weak symbols, strong references, and weak references until I saw the programmer's self-cultivation-link, load, and library. When I see 3.5.5 Weak sign and strong sign, I feel a bit confused, so write this article, I hope to communicate with the same feeling of friends also hope high-skilled pointing.

First, let's take a look at the book's definition of them.

Introduction Scenario: (1) the variable i (int i = 1) is defined and initialized in file A, and the variable i (int i = 2) is defined and initialized in file B. Compile link A, B will error B.O: (. data+0x0): Multiple definition of ' I '; A.O: (. data+0x0): Multiple definition of ' I '. (2) Define and initialize two variables i (int i = 1; int i = 2) in file C and error when compiling link c. C:2:5:error:redefinition of ' I '; c.c:1:5:no te:previous definition of ' I ' is here.

Strong symbols: Symbolic definitions such as those in the scene are called strong symbols, and the compiler default functions and the initialized global variables are strong symbols for C + +.
Weak symbol: After that, the global variable initialized is a weak symbol.
The compiler's rules about strength and weakness symbols are: (1) strong symbols are not allowed to be defined more than once, but strong and weak can coexist, (2) when strength and weakness coexist, strongly covered weakly, and (3) are weak symbols, select the most occupying space, such as choosing a double type instead of the int type.

Defined by the above so there are scenes I did not think of before:
Code A.C:

1 int 2;

Code B.C:

#include <stdio.h>int  i; int Main (intChar* * argv)      {printf ("i =%d\n" ) , i);       return 0 ;      }

Compile the files A and B and link, the result output I is 2 instead of 0.
And the same file is defined but not initialized two identical variables will not error, only when the use of variables will be error.
For the GCC compiler, it is also possible to use __attribute__ ((weak)) to define strong symbols as weak symbols, which are already
Code C.C

1#include <stdio.h>2  3__ATTRIBUTE__ ((weak))inti =1;4  5  intMainintargcChar**argv)6  {7printf"i =%d\n", i);8       return 0; 9}

The output of result I is still not 2 instead of 1.

So is it true for functions? Instead of looking at the function, we first look at the strong and weak references that are further introduced by the strong and weak symbols. A summary of the strength and weakness of the book is that the strong reference is not defined when the link will be an error, and the weak reference will not be error, the linker default to 0 (this is good understanding of the function, that is, the function symbol represents the entry address is 0; for the variable to be aware, since it is referring to the natural address, So the same function as the variable's address is 0 instead of the value of the variable is 0). Is there no clear concept of strong or weak references at this time? What exactly is a reference? What is the relationship between references and symbols? Here I say my understanding (please correct), the function name, the variable name specified in the definition and declaration is the corresponding symbol, and when calling a function or using a variable elsewhere in the code, the letter description and the variable name are considered references, so that symbols and references are actually a thing on the code level. Just according to the environment and called different. Then strong symbols are referenced to Chingqiang, and weak symbols correspond to weak references.

The characteristics of the strong and weak references above can be seen, when a function is a weak reference, regardless of whether the function has no definition, the link will not error, and we can determine whether the function name is zero to determine whether to execute the function. In this way, the libraries that contain these functions can be combined with our references in modules, plug-ins, and with our reference, and since strong symbols can cover weak symbols and the relationship between strength and weakness, we can define our own functions to override the functions in the library.

Look at the criteria to determine if the function is executed:
Code D.C

1  #include <stdio.h>2  3void  func ()4{5      printf ("func () #1 \ n"); 6 }

Code E.C

1#include <stdio.h>2  3__ATTRIBUTE__ ((weak))voidfunc ();4 5 intMainintargcChar**argv)6 {7      if(func)8 func ();9      return 0;Ten}

Compile the d.c,cc-c D.C output D.O; compile e.c and link d.o,cc d.o e.c-o e output executable e, run e normal execute function func. Compile e.c but do not link d.o, this time does not error, but Func will not execute, because there is no definition of it so if (func) is false.
Then look at the function overlay:
Code F.C

1 #include <stdio.h>23void  func ()4{5       printf ("func () #1 \ n"); 6 }

Code G.C

1#include <stdio.h>2  3 voidfunc ()4 {5printf"func () #2 \ n");6  }7  8 intMainintargcChar**argv)9 {Ten func (); One      return 0; A } -~

Compile the link, structure output "func () #2".

The above can be explained that the function and variables are consistent, in fact, the strain can also be used as a function to judge the use of the first, but not to determine the value of the variable, but the address of the variable, such as
Code V1.C

int 2;

Code V2.C

1#include <stdio.h>2  3__ATTRIBUTE__ ((weak))extern inti;4 5 intMainintargcChar**argv)6 {7      if(&i)8printf"i =%d\n", i);9     return 0;Ten } One~

Output 2 when compiling and linking v1, but no output when compiling but not linking v1. In doing so, distinguish between definitions and declarations, __attribute__ ((weak)) int i is the definition of a variable and converted to a weak symbol, so that I is allocated space, and __attribute__ ((weak)) extern int I The original defined variable i is converted to a weak symbol by a strong symbol, resulting in a weak reference instead of a strong reference when I is used. But although variables can do this, there is no function that makes sense.

The above-mentioned strength reference still uses the GCC-provided __attribute__ (weak), and the book mentions __attribute__ (Weakref), which seems to more reflect the "citation" of the keyword. The reason why I use the former to introduce strong and weak references is because I understand the relationship between strong and weak symbols and strong references. About the use of __attribute__ ((Weakref)), here is one (both have different ways of using).
Code A.C

1 #include <stdio.h>23void  bar ()4{5      printf ("foo () \ n"); 6 }

Code B.C

1#include <stdio.h>2 3 Static voidFoo () __attribute__ ((Weakref ("Bar")));4 5  intMainintargcChar**argv)6 {7      if(foo)8 foo ();9 Ten      return 0; One}

Note that the static modifier of the function foo, if not, will be error-free, so that the function foo is restricted to use in this file only.

Well, the night has been deep, writing a little messy, I also messy.

Some of the ugly views about strong, weak, strong, and weak references in C language, please correct me.

Related Article

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.