Linux C Notes Variable storage type detailed

Source: Internet
Author: User

Storage Type:
The storage type of a variable is the type of memory that stores the value of the variable;
    The storage type of the variable determines when the variable is created, when it is destroyed, and how long it will be stored;
Storage variables: Normal Memoryrun-time stackHardware Registers
The default storage type of a variable depends on its declaration location;
Static variables:variables declared outside the code block are stored in static memory, that is, memory that is not part of the stack.
a static variable that cannot be specified for another storage type. It is created before the program runs and always exists throughout the execution of the program. It is always keep the original value unless you assign him a different value or the program ends.
Auto (Automatic) variable: The default storage type of a variable declared inside a code block, stored on the stack. keyword AutoUsed to decorate this type of storage, but rarely used.
In the program when you execute a block of code that declares an automatic variable, the automatic variable is created, and when the program's execution flow leaves the code block, these automatically change The amount of their own destruction. If the code block is called repeatedly, these automatic variables are recreated each time. When code blocks are executed again, these automatically changeThe amount of memory occupied in the stack may be the same as the original position, or it may be different. Even if they occupy the same position, you cannotensure that this memory does not have other uses at the same time. Therefore, we can say that the automatic variable disappears after the code block executes, and when the code block is , their values are generally not the last value executed.     If you add static to a variable that is declared inside a code block, you can make its storage type automatically become static. A variable that has a static storage typeThe volume is present throughout the execution of the program, not just when the code block that declares it executes. Note:Modifying the storage type of a variable does notindicates repairchanging the scope of the variable, it can still only be accessed by name within the code block. The formal parameter of a function cannot be declared static because the argumentalways inpass parameters in the stack to support recursion.
Register (Register) variable:
    stored in the machine's hardware register instead of in memory.
    register variables are more efficient to access than variables stored in memory. However, the compiler does not necessarily "follow" the Register keyword, if there is tooMany of the variables are declared as register, it only selects the first few are actually stored in the register, the rest is processed by ordinary automatic variables.

in a typical case, you would certainly want to declare the most frequently used variables as register variables. In some computers, the efficiency of the program can be improved if the pointer is declared as a register variable, especially for those pointers that frequently perform indirect access operations. You can declare the parameter of a function as a register variable, and the compiler will generate instructions at the beginning of the function to copy the values from the stack to the registers. However, it is possible that the cost of time and space saved by this optimization is not offset by the overhead of replicating these values. register variables are created and destroyed at the same time as automatic variables, but it requires some extra work, before a function that uses a register variable returns, the values previously stored by these registers must be restored , ensure that the caller's register variable is not corrupted. Many machines use the run-time stack to accomplish this task. When the function starts executing, it saves the contents of all registers that need to be used on the stack, and when the function returns, the values are then copied into the register. in the hardware implementations of many machines, the register variable is not assigned an address. Similarly, because of the preservation and recovery of register values, the values that a particular register holds at different times are not necessarily the same. Therefore, the machine does not provide the address of the register variable as you provided.  


From for notes (Wiz)

Linux C Notes Variable storage type detailed

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