Stingy Static, strong Const, puzzled Typedef, as well as vigilant Volatile

           Learn C program for a semester, and sometimes encounter a lot of programs appeared above the "Static" "Const", did not figure out how, are muddling along, and today access to the relevant information, and finally put them distinguish you, ooh ooh view the information process and fortunately learned something new typedef and Volatile ... this nonsense, the next step is to share time:

       Static: Chinese called "static local variable" modified Static variables can only be used in this module, can not be called outside a function or module, any access outside of this module is illegal ... ordinary variables in the implementation of End after the function, its memory space will be released, but the memory space of the modified Static variables will not be released, for example:

{

        unsigned uchar A=0;

        static unsigned char B=0;

        A++;

        B++;

}

Each time the function is called when the A ++ is executed, its value can only be 1; but B is different, he would have been added on until added to 255, maximum overflow becomes 0, although the function is called time, but the B-initialized action will be skipped.

       Const: Const is an abbreviation for Constant (Constant), in fact, be more accurate to say Const can not be modified, is very strong, is always the same, after once stated can not be changed, talked about the C program, would have talked pointer, of course, it can also be modified Const pointers, but in the end is the content pointed to by modifying it? Or pointer variables?

Ooh ooh, that it is necessary to comply with the principle of proximity:

       const unsigned char * str; const data type from the more recent, so he modified the data type, that is, the memory cell content * str points to can not be modified.

       unsigned char const * str; const pointer from the more recent, the pointer variable is fixed and can not be modified, in which case the pointer points to the memory cell which is fixed ~~~ However, it is the content that can be modified ~~~

       There comes a memory space, the total storage space to say about the microcontroller, AVR has a register, SARM, E2PROM, as well as FLASH, ease of understanding, it will be understood as a computer's memory SRAM, FLASH can be understood as the computer's hard disk, memory decisive than the hard disk does not know how many times less, so in general const modified constant FLASH, and FLASH, E2PROM, SRAM addressing them are independent, there is no relationship between the three addresses, in which case the pointer it can not be used indiscriminately, generally because of the Const are modified in FLASH, he can not point to a modified pointer SRAM, or the E2PROM ..

       typedef: typedef commonly used in the custom variable types, for example:

                                                            #define uint unsigned int

                                                            uint number=0;

or it could be:

                                                            typedef unsigned int  uint

                                                            uint number=0;

        Since both methods are the same, then why bother C program it? In actual fact, please read on:

        #define point_zhizhen   unsigned int *

        point_zhizhen  point_A,point_B;

        At this point he will unfold like? The results are evident:

        unsignede int *point_A,point_B;

                                       The use typedef when a different story ...

       typedef  unsigned int *     point_zhizhen

       point_zhizhen  point_A,point_B;

       At this point we get two of the same data type pointer to it (__ ^ * ^ *) hee ......

        Volatile: Chinese explanation is volatile and unstable. In our editor, we often will code optimization, improve execution speed of the code, on how to optimize the code editor is caused by wrong, please refer to the "layman AVR" in which there are detailed instructions, here to talk about how to avoid the errors generated it, time code editor optimized code will be deep into the memory space, but if there is a statement does not write or read operation, the editor will try to be smart to save a certain time of the memory space the value calculated for the future, although the contents of the memory cell has changed, but the role of the value of the variable or save time, and many more will be some unexpected errors, and Volatile program is used to remind the compiler the contents of this memory unit space is changing, you do not optimize this variable ah ~ ~ ~ Well, of course, the compiler will be obedient so slightly - in the following cases, you must use volatile:

1: For global variables used in the main function loop, if the value thereof may be updated in an interrupt handler

2: For memory-mapped register unit

3: multi-threaded system, shared by multiple threads of variables,

Ooh ooh, write tired, take a rest, to be continued ~ ~ ~

Reproduced in: https: //www.cnblogs.com/chenxukai/archive/2010/08/01/1789951.html

Guess you like

Origin blog.csdn.net/weixin_33885253/article/details/93480312