Journal of C-Scope, lifetime, and memory location of variables

Source: Internet
Author: User

"Heaven and Earth Xuan Huang, the Universe primitive", First has the world, the rear has the ancient and modern.
Variables are the same, with the scope of the variable (where it exists), and then the lifetime of the variable (the time it exists).

"How variables are stored"
The first variable is actually stored in two regions:

    • Static storage: Refers to how the system allocates a fixed amount of storage space in the program run interval.
    • Dynamic storage: Dynamically allocate storage space as needed during the program's run.

"Variable Classification"

  1. Local Variables : variables declared inside [functions].

    Scope: From defining the position of the variable to the end of the function.

    <1> Auto variable (auto variable), which is a dynamic local variable that is not specifically declared as static.
    Classification:<11> function parameters;
    Variables defined in the <12> function;
    <13> field protection and return address of function call;
    Storage location: dynamic storage area;
    "Lifetime": when the function is called, it is assigned the initial value to survive, ending the call to death.
    <2> static local variables (static local variables), through the static Declaration;
    Store Location: static storage area;
    "Lifetime": when the function is called, the initial value is given to survive, and the end call continues to exist
    "Disadvantage": long-term occupancy does not release, more memory, not recommended to use more.
    <3> register variable (register variable), through the Register statement;
    Storage location: Register (Register access speed exceeds memory access speed);
    "Lifetime": the same as static local variables, is more rare.

  2. Global variables (external variables): variables defined outside of [functions].
    "Lifetime": exists in the entire run of the program.
    Scope: From defining the position of the variable to the end of the function.
    "Scope extension"
    <1> can be accessed before the variable is defined, and if you want to manipulate the variable before declaring it, you can use extern, such as:
    extern int A;
    int A; This definition must be an external variable definition
    <2> can be accessed in other files and must be extern when using variables from this file in other files.
    When an extern is encountered, the compiler first looks for the definition of an external variable from this file, and if it does not find the definition of an external variable from another file.
    <3> limits the scope of an external variable to this file (which is available in other files after the default external variable definition), and now we do not want to have access to the variables in this file in other files, and you can declare static before the variable so that the scope is limited to this file.

[defines whether the variable is local or global in the main function]
is a local variable, and the variables inside any function are local.

"Heap and Stack in memory"

  • .[Memory partition]
    <1> Stack Area: The stack address is growing downwards at a relatively high address in the direction of the growth of the address. Allocation in stacks[Auto Variable]
    <2> Heap Area: is an upward growth for allocating programmersthe requested memory space;
    <3> static zone: is assigned[static local variables] [global variables]of space;
    <4> Text constant Area:constant StringThis is where it's released by the system at the end of the program.
    <5> Program code Area: storage[function body]Binary code.
        //Take a look at an online popular classic example: MAIN.Cint a =0; Global initialization Zonechar *P1; Global uninitialized area main () {Intb StackChar s[] ="abcchar *P2; stack char *p3 = Span style= "color: #800000;" > "123456"; 123456\0 in the constant area, p3 on the stack. static int c =0char *) 10); Heap P2 = (char *) malloc (20 ); Heap}                 
  • [note]:
        <1> Heap is allocated by the user, with the Malloc,calloc function request, the corresponding free to release, or redistribution with realloc.
             Note: The base type of the Malloc/calloc/realloc function (the type referred to by the returned pointer) is a void (indeterminate) type, The type must be converted to the desired base type at the time of use. The dynamic allocation of memory is primarily used to build dynamic data structures in programs, such as linked lists. The
        <2> stack is automatically assigned by the system, and when the subroutine is called, the return address is pressed into the stack and the return address is taken from the top of the stack when exiting. If a local variable is defined in a subroutine, these variables are allocated on the stack, which is used to withdraw the stack when the subroutine exits, and the local variable is invalidated.
     void sub () {int a[100]; // local variables, allocated on the stack, exit subroutine auto-release. int *p = (int *) malloc ( Span style= "color: #800080;" >100); // allocated on the heap, need to be released with free. }

"Relationship between declaration and definition of a variable"
A declaration of the creation of a storage space is defined (int a=4;), and a declaration of the non-established storage space is declared (extern int A;);

"Intrinsic and external functions"
Functions are inherently external and can be accessed by external functions and files in external folders.

      • Intrinsic functions
        We don't want the function to be accessed by the file in the external folder, so we use static to decorate the current function. can only be accessed in this document.
      • External functions
        The function in the external file access is conditional, that is, when defining the function is preceded by extern, so that we can be in the folder under the file in the other functions of the specific implementation of the function, including the use of this method, import a large class library, we can call the class library functions.

Journal of C-Scope, lifetime, and memory location of variables

Related Article

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: 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.