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 use the keyword Static in C language

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

Share

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

This article will explain in detail how to use the keyword Static in c language. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Memory allocation of executable programs

1. Executable program segmentation

Three basic segments of a program: text segment, data segment, bss segment

BSS BSS (Block Started by Symbol) usually refers to an area of memory used to store uninitialized global and static variables in a program.

The feature is that it is readable and writable, and the BSS segment will automatically clear 0 before the program is executed.

Therefore, the non-initial global variable is 0 before the program executes.

Note the difference from data segments. BSS stores uninitialized global variables and static variables, while data segments store initialized global variables and static variables.

Under UNIX, you can use the size command to view the segment size information of the executable file. Such as size a.out.

Data segment. Data stores data that can be determined at compile time (not at run time) and is readable and writable.

Also known as static storage, global variables with initial values and static variables with initial values are stored in this area, and constants also exist in this area. Data segment, the code segment is determined before the program runs.

A code snippet. Text code snippet is usually an area of memory used to store program execution code.

The size of this area is determined before the program runs, and the memory area is usually read-only, and some architectures also allow code snippets to be writable, allowing self-modification of the program.

In the code snippet, it is also possible to include some read-only constant variables, such as string constants.

The text segment is determined at compile time that the memory is mapped to read-only, but the date segment and the bss segment are writable.

2. Five major memory partitions in c language

1. Stack area (stack area stack)

The stack is automatically allocated and released by the compiler, storing the parameters of the function and the values of local variables (auto type) in a manner similar to the stack in the data structure. Stack application is automatically allocated by the system, such as applying for a local variable int h inside the function, judging whether the applied space is less than the remaining space of the stack, and opening up space for it to provide memory for the program, otherwise an exception will be reported to prompt the stack overflow.

two。 Heap (heap)

The heap is generally allocated and released by the programmer, and if the programmer does not release it, the end of the program may be reclaimed by OS.

It is different from the heap in the data structure. The allocation method is similar to the linked list, and the application is done by the programmer himself using malloc or new.

The application process is complicated. When the system receives an application from the program, it traverses the linked list of free memory addresses to find the first heap node whose space is larger than the applied space, then removes the node from the list of idle nodes and allocates the space of the node to the program. In some cases, the first address of the newly applied memory block records the size of the allocated memory block. This will release the memory space correctly when free ().

3. Global static storage area

Global variables and static variables are stored together, initialized global variables and static variables are stored in the same area, uninitialized global variables and uninitialized static variables are stored in another adjacent area.

4. Text constant area

The constant string is placed in this part, the read-only storage area, and is released by the system at the end of the program.

5. Program code area

The binary code area in which the program is stored.

The difference between the two is: code segment, data segment, stack segment is the concept of cpu level, the five major partitions belong to the concept of language level, the two are different concepts.

3. Mapping and Partition of Executable Program memory Space and logical address Space

On the left is the execution file of the UNIX system, and on the right is the division of the logical address space corresponding to the process.

4. Give an example

2. Static variable

Static variables mainly distinguish between static global variables and global variables, local variables and static local variables.

1. Static global variable, global variable

The difference between static global variables and global variables is mainly through life cycle and scope.

a. Static global variables and global variables are stored in the data segment .data

b. The static local variable is defined within the function, and the lifetime is the entire source program, but the scope is the same as the automatic variable and can only be used in the function that defines the variable. After exiting the function, although the variable continues to exist, it cannot be used.

c. If the static local variable of the basic type is not given an initial value at the time of description, the system is automatically assigned a value of 0. If an automatic variable is not assigned an initial value, its value is indefinite.

d. The global variable itself is static storage mode, and static global variable is also static storage mode. But their scope, the scope of non-static global variables is the entire source program (multiple source files can be used together), while static global variables limit their scope, that is, they are only valid in the source file in which the variable is defined. it cannot be used in other source files of the same source program.

Global variable instance

The following is the b.c and a.c source code

Global variable

Compile

Gcc a.c b.c

Execution result:

Execution result

From the compilation results, the file a.c can access the static global variable b in the b.c file.

Static global variable instance

Compilation result

Compilation result

As can be seen from the compilation results, the file a.c cannot access the static global variable b in the b.c file, so the compilation error is reported.

two。 Static local variable, local variable

The difference between static local variables and local variables is mainly through the life cycle and scope.

Static local variables are stored in the data segment .data, and local variables are on the stack; both static local variables and local variables can only be accessed inside the function body.

The static local variable accessed by the function each time, and the value of the variable is the modified value of the last access.

For example:

1 # include 2 34 void func () 5 {6 int aa= 11; 7 8 printf ("aa=% d\ n", aa++); 9 10} 11 12 int main (int argc, char * * argv) 13 {14 15 func (); 16 func (); 17 18 return 0; 19}

For ordinary local variables, they are initialized once in the stack each time they are called.

1 # include 2 34 void func () 5 {6 static int aa= 11; 7 8 printf ("aa=% d\ n", aa++); 9 10} 11 12 int main (int argc, char * * argv) 13 {14 15 func (); 16 func () 17 18 return 0; 19}

The static variable aa in the function is initialized only once, and the value accessed each time should be the result of the last processing when the function was last called.

3. Static function

1. Concept:

The function is defined as a static function by adding the keyword static before the return type of the function.

The definition and declaration of the function is extern by default, but the static function is only visible in the file that declares it and cannot be used by other files.

Static functions (also called internal functions) can only be called by functions in this file, not by functions in other files of the same program.

Different from the general non-static function (external function) static can be used to modify variables or functions in c.

First look at the time when it is used to modify variables. Variables can be divided into global data areas, stacks and heaps in c.

In fact, what we usually call a stack is a stack rather than a heap, so don't get confused.

two。 The benefits of defining static functions:

Functions with the same name can be defined in other files without conflicts, and you don't have to worry about whether the functions defined by yourself will have the same name as the functions in other files, because it doesn't matter with the same name.

Static functions cannot be used by other files. The storage specifier auto,register,extern,static corresponds to two storage periods: automatic storage period and static storage period.

The count function declares a local variable of the function and sets it to the static type as a counter so that the function can be counted each time it is called. This is the best way to count the number of times a function is called, because this variable is closely related to the function, and the function may be called in many different places, so it is difficult to count from the caller's point of view.

Static functions are automatically allocated in a storage area that is always in use until you exit the application instance, avoiding pushing the stack off the stack when calling the function, which is much faster.

Give an example

A.c

1 # include 2 3 void func (); 4 5 int main (int argc, char * * argv) 6 {7 8 func (); 9 10 return 0; 11}

B.c

1 # include 2 3 int b = 10; 4 56 static void func () 7 {8 printf ("in func b =% d\ n", b); 9}

This is the end of the article on "how to use the keyword Static in c language". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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