2223 views|4 replies

6580

Posts

0

Resources
The OP
 

Advantages of static functions [Copy link]

Static function Add the keyword static before the return type of the function, and the function is defined as a static function. The definition and declaration of a function are extern by default, but a static function is only visible in the file in which it is declared and cannot be used by other files. Benefits of defining static functions: <1> Functions with the same name can be defined in other files without conflict <2> Static functions cannot be used by other files. The storage specifiers auto, register, extern, and static correspond to two storage periods: automatic storage period and static storage period. auto and register correspond to automatic storage period. Variables with automatic storage period are created when entering the program block that declares the variable. They exist while the program block is active and are destroyed when exiting the program block. The keywords extern and static are used to describe variables and functions with static storage period. A local variable declared with static has a static storage duration, or static extent. Although its value remains valid between function calls, the visibility of its name is still limited to its local scope. A static local object is initialized for the first time when the program executes to the declaration of the object. Due to the above characteristics of static variables, some specific functions can be implemented. 1. Counting times Declare a local variable of a function and set it to static type as a counter so that it can be counted every time the function is called. This is the best way to count the number of times a function is called, because this variable is closely related to the function, and the function may be called in multiple different places, so it is difficult to count from the caller's perspective. Benefits of using static functions in C language: Static functions will be automatically allocated in a storage area that is always used until the application instance is exited, avoiding stack push and pop when calling the function, which is much faster. The keyword "static" means "static" in Chinese, so internal functions are also called static functions. But here "static" does not refer to the storage method, but to the scope of the function being limited to this file. The advantage of using internal functions is that when different people write different functions, they don't have to worry about whether the functions they define have the same name as functions in other files, because it doesn't matter if they have the same name.

This post is from Microcontroller MCU

Latest reply

Static local variables are placed in the global static area, right? This should not be pushed onto the stack. Judging from what the OP said, it means static functions. What I understand is static local variables, but beginners may be misled after reading it. It is better to be rigorous.   Details Published on 2018-6-18 10:58
 

6423

Posts

17

Resources
2
 
Static functions are automatically allocated in a storage area that is always used until the application instance is exited, which avoids stack push and pop when calling the function, and is much faster. Do static functions not need stack push and pop? Does it mean that functions do not need stack push and pop or static local variables do not need stack push and pop? I feel there is ambiguity here. Only inline functions do not need stack push and pop.
This post is from Microcontroller MCU

Comments

The static function mentioned by the OP should refer to the static variable. However, when this variable is automatically allocated space on the stack when the function is called, the time is negligible, because whether there is this variable or not, there will be a constant time for the function call to enter and exit the stack.  Details Published on 2018-6-18 10:52
 
Personal signaturetraining
 

6040

Posts

204

Resources
3
 
Bai Ding posted on 2018-6-18 08:35 Static functions will be automatically allocated in a storage area that is always used until the application instance is exited, avoiding stack push and pop when the function is called, and the speed is much faster...[/quote] [quote]Static functions will be automatically allocated in a storage area that is always used until the application instance is exited, avoiding stack push and pop when the function is called, and the speed is much faster.
The static function mentioned by the OP should refer to the static variable, but when this variable is automatically allocated space on the stack when the function is called, the time is negligible, because whether there is this variable or not, there will be a constant time for stack push and pop when the function is called.
This post is from Microcontroller MCU

Comments

Static local variables are placed in the global static area, right? This should not be pushed onto the stack. From what the OP said, it means static functions. I understand it as static local variables, but beginners may be misled. It is better to be more rigorous.  Details Published on 2018-6-18 10:58
 
 
 

6423

Posts

17

Resources
4
 
lcofjp posted on 2018-6-18 10:52 The static function mentioned by the OP should refer to static variables, but when this variable is automatically allocated space on the stack when the function is called, the time is negligible...
Static local variables are placed in the global static area, right? This should not be pushed onto the stack. Judging from what the OP said, it means static functions. What I understand is static local variables, but beginners may be misled after reading it. It is better to be rigorous.
This post is from Microcontroller MCU

Comments

agree  Details Published on 2018-6-19 22:19
 
Personal signaturetraining
 
 

6580

Posts

0

Resources
5
 
Bai Ding posted on 2018-6-18 10:58 Static local variables are placed in the global static area, this should not be pushed onto the stack. I only see that the host means static functions. I understand static local...
Agree
This post is from Microcontroller MCU
 
 
 

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list