C + + function scope __jquery

Source: Internet
Author: User
Tags variable scope

Variable scope

Local scope, global scope, file scope

Local Variables : Also called internal variables, declared within the function (including the main function), the scope begins at the declaration of the variable, ending at the end of the function or block;

Global Variables : Also called external variables, declared outside the function, the scope is generally the entire program source file, the scope is broadest, and can even make all the source files that compose the program (for example, the global variable declared in a source file when linking multiple independently compiled source files to a program). can also be used in other source files that are linked to the source file, but must have an extern global declaration before use. In this way, all functions in multiple source files can use global variables, changing the value of a global variable in one function to all other functions that use the global variable, which is equivalent to a channel between functions that transmits data directly. Of course, this also undermines independence)

file scopes : Variables declared outside of a function are available only in the current file scope (including all defined functions within the file) and are unavailable in other files. To make a variable have a file scope, you must add the static keyword before the declaration of the variable. When multiple source files are linked into a program, static can prevent conflicts between a global variable in one file and a variable with the same name in another file.

Variables within the same scope cannot have the same name , but variables within different scopes can have the same name, overwriting/masking variables of the same name as other scopes in this scope. (I'm the boss of my turf)

1. static global variable and ordinary global variable difference.

Add static-> static global variable before global variable declaration, global variable itself is static storage way, static global variable is static storage way, there is no difference in storage mode.

The main difference is the role of non-static global variables and the entire source program (composed of multiple source programs, valid in each source file); a static global variable limits the scope, only valid in the source file that defines the variable (global scope-> file scope) to avoid causing errors in other files. Static The variable is initialized only once, and the next time the value is last .

2. static local variable and ordinary local variable difference.

In the local variable declaration before the static local variable->, storage mode changed, local variable is dynamic storage, static local variable is static storage mode, that is, change its lifetime, by the function inside or within the block into file scope, although not the real "file scope", declarations within a function, but not accessible outside of the function, should only be done to avoid conflicting duplicate names.

PS: Static local variables extend the lifetime of local variables, and static global variables reduce the scope of global variables .

3. the static function differs from the ordinary function.

Static functions differ from normal function scopes, only in this file. Intrinsic functions: functions declared and defined only in the current source file are called intrinsic functions (static), and functions that can be used outside the current source file should be described in a header file or an extern global declaration. The static function has only one copy in memory, and the normal function maintains a copy of each call.

storage type of function

Divided into internal functions (static) and external functions (extern) two categories. Intrinsic functions: can only be called by functions in the same source file; external functions: They may be called by functions in other source files.

function default extern with no storage type defined.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////// //

storage type of variable

Any variable has two properties, data type and storage type. A complete variable declaration, in addition to declaring a data type that represents the property of a variable operation, sometimes requires declaring a storage type that represents the way the variable is stored.

In addition to allocating memory space, there are different types of memory space to allocate.

The storage type of the variable determines how the C + + compiler allocates memory for the variable, and determines the lifetime of the variable.

Accurately, the lifetime and scope of a variable are the attributes that analyze the variable from different angles.

Scope of the variable : space, from the point of view of the code space, which code can be seen, which code is invisible. The key is visibility and availability.

the lifetime of a variable: from the point of execution time of the code, it refers to a period of time during which a variable is created to be undone, that is, the life cycle of a variable. When the variable is allocated memory space, it is in the lifetime, and when the memory space is freed, the variable ends its lifetime. The key is "survival status."

The most obvious is static local variables.

Static local variables use

void Fun ()

{

static int a;//A is a local variable, but has a lifetime of global variables

a++;

cout << "a =" << a<<endl;

}

int main ()

{

for (int i = 0; i < 2; i++)//static variable A is not in scope

Fun ();

}

The result is

A = 1

A = 2

Analysis: If not initialized when declaring a static variable, the initialization value is 0, and the initialization of the static global variable is completed before main executes, and the initialization of the static local variable is performed the first time the declaration statement of the variable is executed after the program is run.

The following example: the first declared local variable (x=1) in MyFunc () is in the subsequent statement block, although it is in a lifetime (because the MyFunc () function block that declares and defines it has not yet ended), but is not in scope because the local variable x (2) is masked (/overwritten) by the same name in the statement block, But once the statement block is over, the lifetime of X (2) is over, and only x = 1 is alive within myfunc (). Local variable x (2), Y (2) is not available after entering the function Funca (), although it survives within the statement block.

void MyFunc () {

int x = 1;

{

int x (2), Y (2);//variable "x = 1" Out of scope

cout << "x =" <<x<< "\ n";

Funca (); Variable x (2), Y (2) loses scope after entering function

}

}

Allocation of memory: automatic allocation, static allocation, dynamic allocation , the occupied areas of memory and variable types are as follows:



Static assignment
: When the memory address is allocated at compile time, the Run-time variable takes up memory and the variable is released when the program ends.

Dynamic allocation : The heap (heap) is used to allocate memory for variables, and the heap uses free storage space other than the stack and static storage area. is a way of controlling memory allocation entirely by the program itself (control includes opening space new, releasing space delete)

For variables that are not dynamic memory allocation, determine which type of memory allocation the variable takes, which is determined by specifying the storage types and the position of the declaration statements in the program when the variable is declared.

Storage classes for C + + variables:

Auto, register, extern, static

You can specify when declaring:

< storage type > < data type > < variable name list >;

auto int A;

static float b,c;

Register and Auto Declaration Local variables: register stored in registers, auto store on stack

extern declares global variables, static declares global variables and local variables

Both extern and static are in the static storage area.

No storage type specified, default is as follows:

Local Variables: Auto

Global variables: extern

Global variables: defining and referential declarations

A definition declaration defines a variable and allocates memory space. Can only be outside the function, generally without extern

A reference declaration indicates that a variable has been declared elsewhere in the program's source file (allocated space). Can be placed outside a function, or within a function, to add extern, generally cannot initialize a variable unless the definition declaration is not initialized.

A program has multiple source files, and if each file uses the same global variable, the declaration of global variables is made in each file, but if it is a defined declaration, compiling each source file is not an error, and linking them is a duplicate definition error. So the method is to make a definition declaration in a source file, and other file reference declaration.

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.