Original link: http://blog.csdn.net/pirateleo/article/details/7529776
First, static
Keyword static, when modifying a variable:
1, limit the scope of the variable: such as static global variables, can only be used in the module (in this C file).
2. Determine where the variable is stored: modified as a static variable, stored in a static data area (not in the stack). (The global variables are also stored in the static data area.) )
Static variable with initial value and without initial value: (Take Ti DSP 54XX for example)
A. Static variables without an initial value, stored in the. BSS segment.
B. A static variable with an initial value, stored in a. BSS segment, but whose initial value is stored in. init. The value of the INIT segment has been assigned to the. BSS corresponding variable after the DSP Booter completes loading initialization.
Keyword static, when modifying a function: Restricts the scope of the function and can only be used in this module (in this C file).
Conclusion: The static keyword is most important for the user to limit the scope of a variable or function.
Second, the const
The keyword const means "unchanging":
1. When to use: when defining a function, if you add a const before the input parameter, you can prevent the input variable from being overwritten and the error that is caused by the change in the function definition, which can act as a function API self-explanatory;
2. How to use:
Here's an excerpt from the classic interview question:
const int A;
int const A;
const int *a;
int * const A;
int const * a const;
The first two functions are the same, a is a constant integer number.
The third means that a is a pointer to a constant integer number (that is, the integer number is not modifiable, but the pointer can).
The fourth meaning a is a constant pointer to an integer number (that is, the integer number pointed to by the pointer can be modified, but the pointer is non-modifiable).
The last one means that a is a constant pointer to a constant number (that is, the integer number pointed to by the pointer is not modifiable and the pointer is not modifiable).
Third, volatile
Keyword volatile means "easy to change":
A variable modified to be volatile, the compiler does not optimize it, and each time it evaluates to the specified address (physical or mapping) to read. This is therefore suitable for "hardware registers of parallel devices (e.g., status registers), non-automatic variables (non-automatic variables) that are accessed in an interrupt service subroutine, and variables shared by several tasks in a multithreaded application", I personally use only in IO and interrupts, There is a post for multi-threaded issues to refer to.
Http://www.soft-bin.com/html/2010/07/30/concurrent-multithread-code-problem-and-volatile-and-atomic-opration.html
Heaps and stacks (heap and stack)
1) The heap is maintained by the user. For example, we malloc a space, the space opened at this time in the heap, call free and then release. Too frequent malloc and free can cause fragmentation in the heap (the address of a space is discontinuous), affecting read and write speeds.
2) Stacks are controlled by the compiler and we do not need to interfere. For example, we define a local variable or call a function. Take the function call as an example, at this time the system will be the field data (mainly part of the register data, such as the PC can be saved at the end of the call program regression here to continue to run) into the stack, the register to meet the function of a series of calculations and control. After the function is executed and then the stack operation is completed, the process continues.
"Go" embedded C language that point (a) several important keywords