[C language learning] functions in C Language

Source: Internet
Author: User

In the code, functions are designed to better implement Modular programming. So what is the essence of a function? How are the variables defined in the function (global variables, local variables, static variables, etc.) stored? Why is the scope and life cycle of global variables different from that of local variables? These questions can be answered only when you have a better understanding of functions!

★Function nature

The essence of a function is a piece of executable machine instruction code. The essence of a function name is a label, indicating the entry of the function code. The value of this label is equal to the first address of the memory space in which the function code is stored.

★Variable ● global variable

Global variables are defined outside the function. Therefore, it does not belong to any function, but only to the source file.

▲Initialization

If a global variable is not initialized, its value is automatically set to 0 by the compiler. Therefore, you do not need to consider the initialization of a global variable.


Running result:



▲Scope and lifecycle

Its scope starts from the line that defines the variable and ends with the source file that defines the variable. During this period, all functions can reference the variable. Note: The scope of the global variable is not from the beginning of the program to the end of the program, but from the beginning of the definition of the variable to the end of the program.

There is A situation where A function A is defined before A global variable is defined in A source file, so A cannot reference this global variable. If you have to reference this variable, declare this global variable before.

● Local variables

A local variable is also an internal variable, which is defined inside a function or a compound statement.

▲Initialization

If the local variable is not initialized, it is dangerous to directly reference it. Because local variables are stored in the memory stack. After a local variable is defined, the compiler will not initialize it to 0, but the original value of the memory space. This value is a random value, and we can imagine it at last. Therefore, you must remember to initialize local variables!


Running result:


▲Scope and lifecycle

Because it is defined inside the function or the compound statement. Therefore, its scope is limited to functions or conforming statements.

★Variable Storage

In the source file, different variables are stored in different regions. A program occupies memory during running, and the memory usage is different. Data segments, code segments, stack segments, and BSS segments

● BBS segment (Block Started by Symbol)

It is usually used to store a region where global variables and static variables are not initialized. BSS segments belong to static memory allocation. Before the program is executed, the BSS segment is automatically cleared. This is why the preceding global variables do not need to be initialized.

● Code segment

Used to store Program Execution Code. This section has been confirmed before the program runs.

● Data Segment

It is usually used to store initialized global and static variables. Static Memory allocation.

● Heap

It is used to store the memory space dynamically allocated by the program. The size is not fixed. Scalability and scalability. When the malloc function is called, the memory is allocated from the stack, and the memory space is expanded. When the free function is called, the memory space on the stack is reduced.

● Stack

It is used to store the local variables defined inside the function (Note: variables that do not include static declarations are generally stored in data segments ). When a function is called, its parameters will also be pushed into the stack that initiates the call. After the call is completed, the return value of the function will also be placed in the stack, because of the advanced and later features of stack, stack is particularly convenient for saving/restoring the call site. In this sense, we can regard stack as a memory zone for storing and exchanging temporary data.

★Function-Related Optimization ● function call Optimization

The function is used to enhance the code module lines, facilitate code reading and modification, and effectively reduce the size of the Code. However, function calling is time-consuming. A function call generally involves four steps:

1. The parameter needs to be pushed into the stack.

2. Register value to be saved

3. The return address needs to be saved.

4. Redirection

The above three steps require access to the memory. It takes a lot of time to access the memory. If the program calls too many functions, it will certainly run slowly. Therefore, unnecessary function calls should be minimized during program execution to speed up program execution.

● Variable storage Optimization

Because local variables are stored on the stack, global variables are stored on the data segment. Different storage locations lead to different lifecycles.

For local variables that are frequently called in a program, the compiler automatically stores them in registers, but the global variables are not stored in registers. Because global variables exist throughout the program, if the global variables are stored in registers, the registers cannot store temporary variables or intermediate values. In computers, register resources are scarce, and programs are not efficient. Therefore, global variables must be stored in the data segment, that is, the memory. Minimizing memory access is also a way to speed up program execution! (The register is in the CPU. It features low capacity and high speed. If some variables are frequently used, they are stored in registers. This will reduce frequent memory operations and save time)


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.