当前位置:网站首页>Summary of the function and usage of const, volatile and restrict
Summary of the function and usage of const, volatile and restrict
2022-07-05 00:29:00 【Kingston】
const
const(constant) Keywords can modify variables 、 Function parameter 、 Return value or function body .
Define read-only variables .
Limiting function parameters . Prevent accidental modification . Increase program security and reliability .
Such as const char *p Express ,p The value pointed to by the pointer cannot be changed , This pointer cannot be used to modify data
Such as char *const p Express ,p The address pointed to by the pointer cannot be changed
Such as const int arr[] Then the values in the array cannot be modified
const char *const p Express , The value and address pointed to cannot be changed
const double locked[4]
double *pc=rates
pc=locked This sentence is illegal . because locked It can't be modified
In the source file const int i = 1 In other documents extern const int i, If in the header file, apply static const int i = 1, Because each file needs to contain this header file , need static Ensure internality . But if you store arrays in this way , Because each file needs to create this static array , It may take up more storage space , We need to consider its advantages and disadvantages from more aspects .
volatile
volatile Qualifiers indicate that variables can be modified by external agents in addition to programs , For example, the variable saves the current clock time .
The compiler optimizes the compilation and puts the unchanged value in the register , For reading , Mark volatile It can ensure that it is not put into the register .
If it cannot be changed by the program but can be changed by the agent const volatile int i.
restrict
restrict Can only be used for pointers int* restrict ptr, So that makes ptr Is the only way to point to a data block .
It can prevent data from being operated by other pointers . If in memcpy In order to prevent overlapping areas , use restrict Modifies the incoming pointer . meanwhile ,restrict It is also conducive to compiler optimization .
author : Hu hahaha
link :https://www.jianshu.com/p/58f618bd9ea3
source : Simple books
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
边栏推荐
- GDB common commands
- Parsing of XML
- Multilingual Wikipedia website source code development part II
- 图解网络:什么是网关负载均衡协议GLBP?
- Recursive execution mechanism
- Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
- P4281 [AHOI2008]紧急集合 / 聚会(LCA)
- 公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
- Hologres query management and timeout processing
- Consolidated expression C case simple variable operation
猜你喜欢
Illustrated network: what is gateway load balancing protocol GLBP?
OpenHarmony资源管理详解
【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)
Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
Date time type and format in MySQL
How to avoid arc generation—— Aafd fault arc detector solves the problem for you
【雅思阅读】王希伟阅读P3(Heading)
Design of emergency lighting evacuation indication system for urban rail transit station
Specification for fs4061a boost 8.4v charging IC chip and fs4061b boost 12.6V charging IC chip datasheet
Leetcode70 (Advanced), 322
随机推荐
Oracle case: SMON rollback exception causes instance crash
【路径规划】RRT增加动力模型进行轨迹规划
Best practice case of enterprise digital transformation: introduction and reference of cloud based digital platform system security measures
Significance of acrel EMS integrated energy efficiency platform in campus construction
Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)
Get to know ROS for the first time
(script) one click deployment of any version of redis - the way to build a dream
[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
What is the difference between port mapping and port forwarding
What did I pay for it transfer to testing post from confusion to firmness?
Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC
Tester's algorithm interview question - find mode
Life is changeable, and the large intestine covers the small intestine. This time, I can really go home to see my daughter-in-law...
Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
It's too convenient. You can complete the code release and approval by nailing it!
uniapp上传头像
The pit of sizeof operator in C language
URLs and URIs
js如何实现数组转树
初识ROS