Objective-C learning Summary-syntax features of functions and variables, objective-c syntax
Objective-C learning Summary-syntax features of functions and variables
Objective-C is an object-oriented language based on C and integrates Smalltalk features. This section describes the C language features of functions and variables in OC.
Global and local variables
Global Variables:
The variables defined outside the function are global variables. global variables can be accessed by all functions in the source file. Their scope is: Starting from the location where the variable is defined and ending the resource program.
Local variable:
Variables defined within a function are local variables. Local variables are valid only within the function and can only be used within the function. These variables cannot be accessed outside the White function.
External global variables and internal global variables
External global variables
Not Defined outside the FunctionStatic, global variables that allow access by other source programs are called external global variables.
Internal global variables
You can useModify the global variable in static mode to prevent other source programs from accessing or modifying the global variable. This is usedStatic global variables are called internal global variables.
Note:Before calling a global variable, you must declare the variables in either of the following situations:
1. Global variables are defined in other source files.
2. Global variables are defined after the call.
Internal and external functions
Internal functions
Used to define a functionStatic modification: this function can only be called by other functions in the current source file. This function is called an internal function.
External Functions
Used to define a functionExtern modifier, or not applicable to any modifier. It can be called by functions in any source file. Such functions are called external functions.
Clang Compilation
Assume thatMain. m andFunction. m two source files, whereThe main. m source file containsMain method, andThe mian function is called.In the function. m source file, you can execute the following command:
Clang-fobjc-arc-framework <framework used> function. m main. m
The preceding commands compile and connect the two source programs to generate an executable file.
Note thatUse in main. m# Import "function. m" to import the specified source file, which meansPut the code in the function. m fileBefore the main. m file, compile it as a whole. Pay attention to the following two points.
1. UseWhen the clang command is compiled, you only need to compileMain. m file.
2.The functions and global variables defined in main. m are regardedMain. m is located in the same file.Static modified internal functions or internal global variables,The main. m program can still call them.
Dynamic and Static Storage
For the memory of the C program, it can be roughly divided into three parts: the program area, the static storage area, and the dynamic storage area.
Static storage:The memory storage mode that the program dynamically allocates as needed during running.
The variables in the C program are either stored in the static storage area or dynamic storage area. The variables in the static storage area are allocated with memory at the beginning of the program running. The variables in the static storage area always occupy the fixed memory during the program running. The static storage area stores the following two types of variables.
Global variables:Both internal and external global variables are saved in the static storage area.Static local variable
。
Dynamic Storage:The program allocates fixed memory storage at the beginning of running.
The bucket where variables in the dynamic storage area are stored is dynamically allocated. When a program calls the same function multiple timesStatic variable) the memory space will be dynamically allocated each time, and the memory space will be automatically released at the end of each function. Such allocation and release are dynamic. If a program calls the same function multiple times, the storage space of the local variables allocated to the function may change each time. The dynamic storage area mainly stores the following three types of data.
The parameter of the function. NonStatic local variable. Function execution site data and return address.
Storage Type
To specify the storage class of a variable, you can specify the storage class when defining the variable. The C language supports the following storage classes.
Auto: Specifies that the variable is automatically stored. Local variables use this storage mechanism by default.
Static: Stores local variables in the static storage area. If you want the value of a local variable to be retained after the function callStatic modifies the local variable. Static local variables are considered as follows:
The variable needs to retain the value at the end of the last call. If you want a variable to be initialized only once and then called, but do not want to assign a value to it again, you can consider using static local variables.
Register: Specify to store the variable in the Register-you do not need to store the variable into the memory, so as to avoid frequent CPU read/write memory, so you can use the frequently used local variablesRegister modifier. HoweverThe variable modified by register can improve performance only theoretically. In fact, there are two restrictions.
The number of registers in a computer is limited. You cannot define any number of register variables. Different SystemThe processing of register local variables is different.Register local variables are stored in registers, and they stillThe register local variable is treatedAuto local variable processing.