C Language Structure

Source: Internet
Author: User

The layout of a process in memory:

. Text segment (body segment)--the program binaries that the program executes, the machine instruction portion of the CPU execution, a program with only one copy, and read-only to prevent the program from modifying its own instructions due to an accident.

. Data Segment --Saves all initialized global variables in the program.

. BSS Segment (non-initialized data segment)--Saves all uninitialized global variables in the program (there are a lot of messy segments in other segments, not tables).

During the entire life cycle of the program, the data in the. die and . BSS sections are the same as the entire program, which means that the data will sanitization to sleep after the program has completely ended.

when a program's global variable is declared as static , its Chinese name is a static global variable. There is no difference between static global variables and other global variables where they are stored, either in the . Data Segment (initialized) or in the . BSS Segment (uninitialized), but it is only valid within the source file that defines it. Other source files cannot access it. So, when the general global variable wears a static coat, it becomes the bride, which has the heart to belong to, can only be defined by its source file (groom) in the variable or function access.

Stack-Growth direction: top-down growth, that is, from high address to low address growth, automatic variables and each time a function call to save information (return address; Environment information).

Heap--Dynamic storage allocation.

Static and Stack relationships:

Static can be used to modify variables in C, or to modify functions.

First look at the time to modify the variables. Variables in c can be divided into existing global data regions, stacks and heaps. In fact, we usually say that stacks are stacks rather than heaps, do not confuse.

int A;

int main ()

{

int b;

int* c = (int *) malloc (sizeof (int));

}

A is a global variable,b is a stack variable, andC is a heap variable ( Dynamically allocated Space ).

Static Modification of global variables can be considered as limiting the only reference to this variable in this file. Some programs are composed of a lot of . C files. You can reference variables to each other, but after adding the static modifier, this variable can only be referenced by the function in this file.

Static modification of the stack variable can be considered as extending the life cycle of the stack variable to the end of the execution of the program. In general, the life cycle of a stack variable is managed by the OS , and the life of the stack variable ends in the process of unwinding the stack. But after adding the static modifier, the variable is no longer stored in the stack, but is stored with the global variable. At the same time, it cannot be used after the function that defines it, but it can continue to be used if the function that defines it is called again, and the value left after the previous call is saved.

Static Modification of a function is similar to a modification of a global variable, and can only be called by a function in this file, not by a function in another file in the same program.

C Language Structure

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.