C language scope, storage period, Link property summary

Source: Internet
Author: User

1. Scope

Block scope: With {} wide, ending from declaration start to '} '

function scope: The scope of Goto (identifier) is the entire function.

Function prototype scope: function declaration begins, function declaration ends

File scope: Entire file or program

2. Links

No link: It is visible only within its scope

Inside Link: That is visible from the declaration to the end of the file

External links: Multiple files or the entire program is visible

3. Storage Period

Static storage period: The entire program is occupied with memory

Thread Storage period: thread_local, which is declared after a separate private backup is assigned to each thread

Automatic storage period: Local variables are usually auto-stored

Dynamic storage period: the memory allocated with new or malloc, if not actively released, the entire program occupies memory

4. Summary

Storage class storage Period scope Link Property representation

Automatic automatic block declaration of variables within a non-link block

Register automatic block in non-link block using register decoration

Static external link static file outer link all functions outside

Static inside link static file links all functions outside, with static decoration

Static non-linked static block in a non-link block, using the static modifier

Thread store automatic threads no link thread_local decoration

4. Storage period of function

External functions: Default are external functions

Static functions: Using static modifiers, inner joins

inline function: inline

5. Attention

    • Auto indicates automatic storage period in C
    • But auto infers the type in c++11
    • After the register is declared, it does not necessarily have to be stored in the register, he is just a request. There is no register in time and no address can be taken. There are not many types that can be saved as registers,

Because some types are too large, registers do not exist.

    • Static variables, or global constants, are stored in the static zone and are assigned when the program is loaded, and if they are not initialized, they will be initialized to 0, in the BSS segment.

6. Apply for Memory

Malloc:malloc (sizeof (XXX))

Calloc:calloc (num,sizeof (XX))

Realloc:realloc (P,size)

7. Qualifiers

Volatile

Const

Restrict

C language scope, storage period, Link property summary

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.