iOS various variables and scopes

Source: Internet
Author: User

Depending on the storage location, it can be divided into static variable (static storage area), dynamic variable (runtime stack), register variable (register)

Static variables (static stores): Global variables and local variables declared with static are static variables that are initialized only once during system operation. #include <stdio.h>

int a=1; Global variables are stored in static memory, initialized only once void ShowMessage () {    static int b=1;//static variables are stored in static memory, and the second call will no longer initialize    int c=1;    ++b;    a+=2;    printf ("a=%d,b=%d,c=%d\n", A,b,c);} int main (int argc, const char * argv[]) {    showmessage ();///Results: A=3,b=2,c=1    showmessage ();//results: A=5,b=3,c=1    return 0;}

Explain:

Global variables, scoped in their own class, can be called everywhere in the class implementation. stored in static zone

Local variables, scopes in a method, method end, variable destruction. stored in the dynamic zone, stored in the stack.

Automatic variable, local variable not containing static, stored in the stack.

Static variable, the scope is this class (if other classes call the variable with the same name, no error is present.) ) modifier function, internal function, external call will error.

extern modified variable: Declares a variable that already exists, is simply declared, not initialized. The extern modifier, which declares and defines functions as external functions, can be omitted, so all functions default to external functions.

A singleton is a global variable, because the singleton is created only once during the entire program run, so the singleton saves only one object, and if you create a singleton in Class A, the singleton still holds the object in Class A, and the various states of the Singleton object still hold the state after the Class A runs. (for example, a player object player, which is a singleton, you set player.playing = YES in Class A, then jump to Class B, create the Player object again, the Player.playing property is still yes and will not default to nil because of re-creation);

Overloading is reloading, or re-alloc.

Summarize

Finally do a brief summary:

    1. extern is used to declare an already defined variable when acting on a variable, but it is not possible to define a variable; using extern you can use global variables in other files (of course, extern can be omitted at this time);
    2. extern acts on a function as if it were a global variable, declaring that the function is an external function, other files can be accessed, but the difference is that when it acts on a function, it can not only declare the function but also define the function (used before the function definition), whether it is a definition or a declaration can be omitted, The C language defaults to that the function definition or declaration is an external function;
    3. When static is applied to a variable, the variable is defined only once and is not redefined when it is used, and when static acts on a global variable, it is indicated that the variable can only be accessed in the current file and cannot be accessed in other files;
    4. Static acting on a function is similar to acting on a global variable, which means declaring or defining the function as an intrinsic function (also called a static function), which cannot be accessed in other files other than the file where the function is located;
    5. Reference http://www.cnblogs.com/kenshincui/p/3854243.html

iOS various variables and scopes

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.