当前位置:网站首页>C language implementation of raii
C language implementation of raii
2022-07-24 19:21:00 【senseshield】
Although there are many disputes , But I still think C++ Medium RAII Idioms are a good thing , Also write C Code is the only thing I miss C++ characteristic . Here are some C Language implementation RAII Methods :
gcc
GCC Top available cleanup Extended implementation
#define RAII_VARIABLE(vartype,varname,initval,dtor) \
void _dtor_ ## varname (vartype * v) { dtor(*v); } \
vartype varname __attribute__((cleanup(_dtor_ ## varname))) = (initval)
void example_usage() {
RAII_VARIABLE(FILE*, logfile, fopen("logfile.txt", "w+"), fclose);
fputs("hello logfile!", logfile);
}
Windows
Windows You can use SEH Of __try/__finally, Words Windows In fact, there are many things that are quite convenient .
standard C
For better performance and portability , You can also use setjmp/longjmp Mechanism , A simple package is as follows , Use TRY/FINALLY that will do
#define TRY do{ jmp_buf ex_buf__; switch( setjmp(ex_buf__) ){ case 0: while(1){
#define CATCH(x) break; case x:
#define FINALLY break; } default:
#define ETRY } }while(0)
#define THROW(x) longjmp(ex_buf__, x) The code above is switch Used in while, It uses Duff Device technology ( As shown below ). It's all because C Not allowed in language goto To switch Any of the statements case It's about .
Duff Device
C-FAQ The form given in is :
int n = (count + 7) / 8; /* count > 0 assumed */
switch (count % 8)
{
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
} This is in C It's legal in language ,switch The only function of a statement is ” Fall ” Start at the right place to start . In the above example, such a comparison can be performed 8 Secondary copy operations , At the same time, don't care count Can not be 8 Division and other problems . This operation is a special loop expansion mechanism (loop-unrolling mechanism).(switch Statement case It's actually a label , This is easy to understand ).
This technology is used in particularly low-level code ( Such as driving ) Or specific needs ( Such as C Language implementation RAII) You can use .
---------------------
author :Virbox Technology blogs
source :Virbox Technology blogs
original text :http://blog.virbox.com/2019/06/25/c-%E8%AF%AD%E8%A8%80%E5%AE%9E%E7%8E%B0-raii/
Copyright notice : This article is an original blog article , Please attach a link to the blog post !
边栏推荐
- Summary of articles in 2020
- 杭电多校第一场第三题 Backpack(异或dp+bitset)
- OpenGL learning (III) glut two-dimensional image rendering
- MySQL hidden version number
- Math
- SATA protocol OOB essay
- Why are there loopholes in the website to be repaired
- [laser principle and application -6]:q switching element and Q drive circuit board
- Detailed explanation of the relationship between MySQL tables
- In the spring of domestic databases
猜你喜欢

asp. Net coree file upload and download example

Day 9 (this keyword and experiment)

Literature reading: gopose 3D human pose estimation using WiFi

Meshlab&PCL ISS关键点

多线程与并发编程常见问题(未完待续)

Pay close attention! List of the latest agenda of 2022 open atom open source Summit

2022杭电多校第二场1009 ShuanQ(数学)

Implement a proxy pool from 0

FPGA 20 routines: 9. DDR3 memory particle initialization write and read through RS232 (Part 2)
![[question 39] special question for Niuke in-depth learning](/img/18/0e182f2c003ff5dd8ed3751c718d73.png)
[question 39] special question for Niuke in-depth learning
随机推荐
Leetcode652 finding duplicate subtrees
Installation and use of lsky Pro lancong drawing bed: a drawing bed program for online uploading and managing pictures
MySQL version 5.7.9 SQL_ mode=only_ full_ group_ By question
[untitled]
Why is gradient the fastest changing direction of function
PostgreSQL weekly news - July 13, 2022
MySQL1
Day 9 (this keyword and experiment)
Timed task framework
【无标题】
OpenGL learning (IV) glut 3D image rendering
Data model subclassing reference
拦截器和过滤器
LTSpice software power settings
Wireshark simple filter rule
Mysql数据库,去重,连接篇
Detailed explanation of the relationship between MySQL tables
Convolutional Neural Networks in TensorFlow quizs on Coursera
Implement a proxy pool from 0
Ebpf verifier