Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to distinguish between scope, memory, and link attributes

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to distinguish scope, memory, link attributes". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Scope

In C language, scope is used to describe which areas can be accessed by identifiers.

The common scopes are as follows:

Block scope, visible from the definition to the end of the block containing the definition

Function scope, the label of the goto statement has a function scope

The file scope is visible from the definition to the end of the definition. Variables defined outside the function have file scope.

Function prototype scope, from the definition of the formal parameter to the end of the prototype declaration

For illustration purposes, let's look at an example that is easy to understand:

/ * * author: Mr. Shouwang Source: official account programming Zhuji personal blog: https://www.yanbinghu.com * / # include int num1 = 2222 / / located outside the function, with file scope static int num2 = 111; / / defined outside the function, with file scope int swap (int * aline int * b); / / the function prototype scope int swap (int * NULL== int * b) {if (NULL== a | | NULL== b) goto error; else {int temp = * a / / defined in the function, block scope * a = * b; * b = temp; return 0;} / / printf ("temp is% d\ n", temp) / / because temp has block scope, the label and function scope of error://goto statements cannot be used directly here, so {printf ("input para is NULL\ n"); return-1;}} int main (void) {printf ("num1=%d,num2=%d\ n", num1,num2) can be referenced earlier. Swap (& num1,&num2); / / num1 num2 has a file scope, and you can use printf ("num1=%d,num2=%d", num1,num2) directly in the main function; return 0;}

As you can see, the error tag has function scope and is visible throughout the function, while temp has block scope, so it cannot be used directly outside the curly braces. Num1 and num2 have file scope, so the main function can use it directly.

Link attribute

In "how hello programs become executable files," we talk about the compilation process, and the last step is linking. The link attribute determines whether identifiers of the same name in different scopes can be bound to the same object or function. In other words, whether the identifiers of different scopes are the same entity after compilation.

The c variable has three link properties:

External links, extern-decorated, or non-static-decorated variables with file scope have external link properties

Internal links, static-modified variables with file scope have internal link properties

Variables without link, block scope, function scope and function prototype scope have no link attributes

To explain a little bit, variables without static modification and with file scope end up bound to the same entity when they are linked with multiple variables with identifiers of the same name. Static-modified variables with file scope are different, and within different files, even if the identifier names are the same, they are bound to different entities.

Therefore, if we want a variable or function to be used only in a file, it is a good idea to use static decorations.

Again, let's look at an example.

/ * * author: Mr. Shouwang Source: official account programming Zhuji personal blog: https://www.yanbinghu.com * / # include int a = 5 / / file scope, external link attribute, other files can use the file's a static b = 6; / / file scope, internal link attribute, even if other files have the same name identifier, they are also different int main (void) {int sum = 0; / no link attribute sum = a + b; printf ("sum is% d\ n", sum); return 0;}

As you can see from the code, both an and b have file scope, a has external link attributes, b has internal link attributes, and sum has block scope, so there are no link attributes.

Storage period

In fact, both scope and link properties describe the visibility of identifiers, while the storage period describes the lifetime of the objects corresponding to these identifiers. Storage periods are also divided into the following categories:

Static storage period, which is always present during program execution, and variables in the file scope have a static storage period

Automatic storage period, which (except for variable-length arrays) starts from the block to the end of the block, so the variable in the block scope has an automatic storage period, which is stored in the stack and needs to be explicitly initialized.

Dynamically allocate the storage period, that is, the variable that allocates memory through malloc. It is stored in the heap and needs to be explicitly initialized.

The thread storage period, as you can see from the name, is related to the thread, and variables declared with the keyword _ Thread_local have a thread storage period, which exists from the declaration to the end of the thread.

For initialization, please refer to the "C language entry Guide-forgotten initialization".

Similarly, we better understand the storage period through the following code

/ * * author: Mr. Shouwang Source: official account programming Zhuji personal blog: https://www.yanbinghu.com * / # include int num1 = 222; / / static storage period static int num2 = 111 / / static storage period int add (int aline int b) {static int tempSum = 0; / static storage period tempSum = tempSum + a + b; return tempSum;} int main (void) {printf ("num1=%d,num2=%d\ n", num1,num2); int sum= 0; / / automatic storage period sum= add (num1,num2); printf ("first time sum=%d\ n", sum) / / sum= 333sum= add (num1,num2); printf ("second time sum=%d\ n", sum); / / sum= 666 return 0;}

In addition, if we look at the symbol table of the compiled program file through the nm command, we can find num1,num2,tempSum, but without sum, the amount of memory used by the former is determined at compile time.

$gcc-g-o lifetime lifetime.c $nm lifetime | grep num1 00000000601038 D num1 $nm lifetime | grep num2 0000000060103c d num2 $nm lifetime | grep tempSum 00000000601044 b tempSum.2289$ nm lifetime | grep sum $

What global variables, local variables, static local variables, static global variables

At this point, we can easily distinguish between the above variable types. In fact, it's just a different way of saying:

Global: variables with file scope

Static: with static storage period or internal link properties

Local: variables with function or block scope

Therefore, when combined, it is easy to understand.

Local variable: a variable in the scope of a function or block

Static local variables: function or block scope, static storage period

Global variables: variables with file scope

Static global variables: internally linked variables with file scope

Of course, this is just to distinguish them, which is not a strict definition of them. A better way is to understand through the code:

# include int num1= 222; / / global variable static int num2= 111; / / static global variable int add (int aline int b) {static int tempSum = 0; / / static local variable tempSum = tempSum + a + b; return tempSum;} int main (void) {printf ("num1=%d,num2=%d\ n", num1,num2); int sum = 0; / / local variable sum = add (num1,num2) This is the end of printf ("first time sum=%d\ n", sum); / / sum= 333 return 0;} "how to distinguish scope, memory, link properties". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report