Storage Class C language and links

Storage Class C language and links

Recent detailed review of the C language, see the storage category when the total feeling of some vague concept, now seriously comb. One of the advantages of C language that allows programmers to control just right, you can specify the scope and lifetime of variables through the memory management system of the C language, to achieve control of the program.

Storage Class

  • basic concepts

Object : In C language, all data will be stored in memory, the stored values will occupy some physical memory, this memory is referred to as a subject , it may store one or more values stored in appropriate value having a size corresponding to a certain. (C language object other than the object of the object-oriented languages)

Identifier : Programs need a way to access the object, which need to declare variables to achieve, for example: int identifier = 1here identifieris an identifier, the identifier is a name and follow the naming rules of variables. Therefore, in the present embodiment, identifieri.e., the specified object is a C program memory hardware manner and provide the stored value of the size "1." In other cases int * pt, , int arr[10]pt is an identifier that specifies the variable to store the address, but the expression * p is not an identifier because it is not a name. arrThe declaration creates a capacity of 10 intobject types of elements, each element of the array is an object.

Scope : Description program accessible area identifier. Because the scope of a variable may be a C block scope, function scope, file scope and function prototypes scope.

Block scope : Block scope is simply a pair of curly code region bracketed. Definition of variables in the block having a block scope, the range is defined to comprise at the end of the definition block.

Function prototype scope : the range is defined at the end of the reference prototype declaration from the shape. We know that the compiler concern only of its type in the handler parameter, and parameter name usually does not matter. E.g:

void fun(int n,double m);     同样可以声明为

void fun(int ,double );

Another point to note is that although the body of the function parameter declaration before the brace, but it has a function that blocks the function body belonging to the scope of the block.

File scope : variables defined outside all functions, from its definition to place at the end of the file are visible call this variable has file scope. So file scope variable is also known as global variables

Links : C variables There are three attributes link: internal links, external links and no link. Has block scope, function prototype scoped variables are not linked variables, which means that they fall within the definition of their private block or function prototype. File scope variables can be external or internal links links, external links may be used in multiple files, it is defined only internal link file unit used.

Storage period

Refers to the object retains much time in memory, the scope and the linker visibility of objects. Storage period is described in the lifetime of access to the object identifier.

There are four C object storage period:

1) static storage period: If an object has static storage period, it has been present during the execution of the program. File scope variables with static storage period, pay attention to keyword staticindicates that the link is not the property of the storage period. To staticfile scope variable declaration has internal links, either with internal links or external links, all file-scope variables with static storage period.

In another case block scope variables can have static storage period, the variable declaration in the block and in the variable name preceded by staticthe keyword, for example:

int fun(int num)
{
    static int Index;
    ...
}

Here variable Indexwas static memory, the program is loaded from memory into the end of the program will continue to exist, but only to enter the program will block access to its specified object.

2) Thread storage period: for concurrent programming, execute a program can be divided into multiple threads, the thread has a variable is declared when the storage period from the end of the thread to persist. Keyword _Thread_localwhen declaring an object, each thread to get back up the private variables.

3) automatic storage of: block scope variables typically have automatic storage period, when the program proceeds to block definitions of these variables, it allocates memory for these variables, leaving the block when the program will automatically release the occupied memory variable, this approach equivalent to an automatic variable memory occupied deemed reusable workspace or staging area.

4) for dynamic allocation of memory

Five kinds of storage class

  • Five kinds of storage class
Storage Class Storage period Scope link Declaratively
automatic automatic Piece No link The block
register automatic Piece No link Blocks within Keywordregsiter
Static External links Static state file Outside All external function
Static internal links Static state file Outside All external function keystatic
Static No link Static state Piece no Blocks within Keywordstatic
  • Automatic variables

Automatic variables belonging to the automatic recognition of automatic storage, and no link block scope. It can be displayed using autokeyword statement.

Note: auto is storage-class specifier auto usage and C ++ are completely different

Variable there is a variable with automatic storage duration means that when the program enters the block, block variable disappears when you quit, the original variable amount of memory for other uses.

void hiding()
{
    int x = 30;
    printf("x in outer block: %d at %p\n", x, &x);
    {
        x = 77;
        printf("x in inner block: %d at %p\n", x, &x);
    }
    // 块中内存被释放隐藏的x恢复 x = 30
    printf("x in outer block: %d at %p\n", x, &x);

    while (x++ < 33)
    {
        int x = 100;
        x++;
        printf("x in while loop: %d at %p\n", x, &x);
    }
    printf("x in outer block: %d at %p\n", x, &x);
}

Without Braces

void forc()
{
    int n = 8;
    printf("  Initially, n = %d at %p\n", n, &n);
    for (int n = 1; n < 3; ++n)
        printf("      loop 1:n = %d at %p\n", n &n);
    // 离开循环后原始的你又起作用了
    printf("After loop 1:n = %d at %p\n", n &n);
    for (int n = 1; n < 3; ++n)
    {
        printf("loop 2 index n = %d at %p\n", n, &n);
        // 重新初始化的自动变量,作用域没有到循环里的n
        int n = 6;
        printf("      loop 2:n = %d at %p\n", n, &n);
        // 起作用的仍然是循环中的n
        n++;
    }
    // 离开循环后原始的n又起作用了
    printf("      loop 2:n = %d at %p\n", n, &n);
}

Output

  • Register variables

Register keyword, stored in the CPU registers stored in the fastest available memory.

  • Block scope static variables

It must first clear the concept of static variables does not mean the value of the variable does not change, but it refers to the same location in memory. Static variables have file scope have static storage period automatically.

Mentioned earlier, we can create a static storage duration, block scope of local variables, such as variables and automatic variables have the same scope, but did not disappear when the program left block,

void trystat();

int main()
{
    int count = 1;
    for (count = 1; count <= 3; count++)
    {
        printf("Here comes iteration %d:\n", count);
        trystat();
    }
    trystat();
    return 0;
}

void trystat()
{
    int fade = 1;
    static int stay = 1;
    printf(" fade = %d and stay = %d\n", fade++, stay++);
}

Output:

As can be seen the value of each variable fade away from the block will be re-initialized, and stay only at compile function void trystat()time is initialized once, in increments after leaving the circulation function block their own body mass and for, explained stay visit there has been an object and not as automatic as variables are released.

  • Static variable external links

With external linkage, static storage duration and file scope variables in that category are external variables. Just declare variables should be placed outside all functions of an external variable is created. In order to show that the function uses external variables, use the keyword externto declare again. If the external variables used in a source file declared in another source file, you must use the extern to declare.

Initialization of external variables can be displayed, if there is no default will be initialized to zero.

  • Static variables internal links

It has file scope static storage duration and internal links, in all external functions staticto declare a static variable is a variable internal links.

Example:

static int val = 1;
int main()
{
    ...
}

A normal external variables can be used at any one function in the program, but static variables for internal links can only function with a file. Can be used externspecifier, any duplicate file scope variables declared in a function does not change their link properties.

Example:

int global = 1;
static int local_global = 2;
int main
{
    extern int global = 1;
    extern int local_global = 2;
    ...
}

Only the difference between internal and external links of links in a multi-file in importance.

To summarize storage class specifier keywords in a total of six auto, register, _Thread_local, static, externand typedefwhere staticand externmeanings depending on the context,

Guess you like

Origin www.cnblogs.com/TJTO/p/11795786.html