惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Palo Alto Networks Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
S
Schneier on Security
S
Securelist
酷 壳 – CoolShell
酷 壳 – CoolShell
C
CXSECURITY Database RSS Feed - CXSecurity.com
Cyberwarzone
Cyberwarzone
Apple Machine Learning Research
Apple Machine Learning Research
S
SegmentFault 最新的问题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
GbyAI
GbyAI
Security Latest
Security Latest
Last Week in AI
Last Week in AI
Microsoft Security Blog
Microsoft Security Blog
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
Webroot Blog
Webroot Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
C
Cisco Blogs
博客园 - 【当耐特】
Blog — PlanetScale
Blog — PlanetScale
Hugging Face - Blog
Hugging Face - Blog
B
Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Attack and Defense Labs
Attack and Defense Labs
The Last Watchdog
The Last Watchdog
U
Unit 42
阮一峰的网络日志
阮一峰的网络日志
Project Zero
Project Zero
WordPress大学
WordPress大学
L
LINUX DO - 最新话题
F
Fortinet All Blogs
L
LINUX DO - 热门话题
PCI Perspectives
PCI Perspectives
Simon Willison's Weblog
Simon Willison's Weblog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
MongoDB | Blog
MongoDB | Blog
Latest news
Latest news
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
The Hacker News
The Hacker News
爱范儿
爱范儿
O
OpenAI News
J
Java Code Geeks
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - cpuimage

AIGC图像困局 深度学习优化器算法巧思速览 lora训练之偷师 大话AI绘画技术原理与算法优化 Tensorflow2 深度学习十必知 自 TensorFlow 2.6 起,修正IDE代码自动补全失效的方法 开源发丝分割数据集CelebAHairMask-HQ(国庆献礼) 博文大纲 文字渲染一探 磨皮美颜算法 附完整C代码 Windows下C,C++开发环境搭建指南 一种简单高效的音频降噪算法示例(附完整C代码) 算法踩坑小记 音频算法之我思 wav2midi 音乐旋律提取算法 附可执行demo 图像去模糊算法 循序渐进 附完整代码 音频算法之小黄人变声 附完整C代码 音频识别算法思考与阶段性小结 大话音频变声原理 附简单示例代码
c语言智能指针 附完整示例代码
cpuimage · 2018-08-19 · via 博客园 - cpuimage

是的,你没有看错,

不是c++不是c#,

就是你认识的那个c语言。

在很长一段时间里,c的内存管理问题,

层出不穷,不是编写的时候特别费劲繁琐,

就是碰到内存泄漏排查的各种困难,

特别在多线程环境下,就难上加难了,

诸如此类的老大难问题。

c++用它的RAII机制妥妥影响了一代程序员。

RAII大概介绍下,就不做科普,

有需要的同学,百度一下了解细节。

什么是RAII

资源获取即初始化 (Resource Acquisition Is Initialization, RAII),RAII是一种资源管理机制,资源的有效期与持有资源的对象生命期严格绑定,即由对象的构造函数完成资源的分配,由析构函数完成资源的释放,总结一句话就是 用对象来管理资源

RAII实现原理

当一个对象离开作用域的时候就会被释放,会调用这个对象类的析构函数,这都是自动管理的,不需要我们手动调用。所以我们可以把资源封装到类的内部,当需要用资源的时候初始化对象即可,当对象被释放的时候资源也会被释放

当你写了多年c代码,你是多么渴望有这么一个东西可以给到你。

众望所归,终于gcc编译器开了个小灶,留了一个后门造福c程序员。

详情见:

https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html

主要看这个:

cleanup (cleanup_function)
The cleanup attribute runs a function when the variable goes out of scope. This attribute can only be applied to auto function scope variables; it may not be applied to parameters or variables with static storage duration. The function must take one parameter, a pointer to a type compatible with the variable. The return value of the function (if any) is ignored.

If -fexceptions is enabled, then cleanup_function is run during the stack unwinding that happens during the processing of the exception. Note that the cleanup attribute does not allow the exception to be caught, only to perform an action. It is undefined what happens if cleanup_function does not return normally.

这个cleanup机制,用起来,妥妥就是一个c的析构函数了。

没有必要造轮子,轮子已经造好了。

libcsptr提供了常用智能指针的封装,

unique_ptr, shared_ptr ,绝对是够用了。

项目地址: 

https://github.com/Snaipe/libcsptr

花了点小时间编写示例代码,造福大家。

顺手解决vs的编译问题。

另外说一下,vs不是gcc,没有cleanup 可以实现这个功能。

不过可以通过安装llvm在vs里选择llvm编译进行编译。

https://llvm.org/releases/download.html

下载后安装,再启动vs就可以看到编译器选项了。

贴个图上来,可参照一下。

选好之后,就可以玩起来一些clang特性了。

完整示例代码:

#include <stdio.h>
#include "csptr_smart_ptr.h"

struct BufferBody {
    char *buffer;
    size_t size;
};

static void callback_dtor(void *ptr, void *meta) {
    (void) meta;
    struct BufferBody *ctx = ptr;
    if (ctx->buffer != NULL)
        free(ctx->buffer);
}

struct BufferBody *write_buffer(const char *bufbody, size_t init_body_len) {
    smart struct BufferBody *ctx = shared_ptr(struct BufferBody, { 0 }, callback_dtor);
    if (!ctx) // failure to allocate
        return NULL; // nothing happens, destructor is not called

    if (ctx->buffer == NULL) {
        ctx->buffer = malloc(init_body_len);
        if (ctx->buffer != NULL)
            ctx->size = init_body_len;
    } else {
        if (ctx->size < init_body_len) {
            ctx->buffer = realloc(ctx->buffer, init_body_len);
            if (ctx->buffer != NULL)
                ctx->size = init_body_len;
        }
    }
    size_t buflen = strlen(bufbody);
    if (ctx->size > buflen)
        memcpy(ctx->buffer, bufbody, buflen);
    return sref(ctx); // a new reference on bufCtx is returned, it does not get destoyed
}

void do_something(size_t init_body_len) {
    smart struct BufferBody *ctx = write_buffer("hello smart ptr.", init_body_len);
    printf("%s \n", ctx->buffer);
    // ctx is destroyed here
}

int main(void) {
    printf("Smart pointers for the (GNU) C\n");
    printf("blog: http://cpuimage.cnblogs.com/\n");
    printf("tips: u can use llvm to compile in visual studio");
    printf("download llvm: http://releases.llvm.org/download.html");
    // some_int is an unique_ptr to an int with a value of 1.
    smart int *some_int = unique_ptr(int, 1);
    printf("%p = %d\n", some_int, *some_int);
    size_t init_body_len = 4096;
    do_something(init_body_len);
    // some_int is destroyed here
    return 0;
}

非常简单,代码严谨性不深究,大家看下示例的具体用法就可以了。

我都觉得简单得写注释都有点多余。

由于原项目文件挺多的,编译挺麻烦的。

就操刀简单修改了一下,

主要是将代码合为一个文件csptr_smart_ptr.h,附示例代码,干净便携。

对应项目地址:

https://github.com/cpuimage/libcsptr

只能说,有了它,你可以省下不少c内存管理心了。

当然会有很多人质疑说,会不会有大坑,

也许会有,也许没有,但是c智能指针的确可以有。

我比较相信事实,当然事实就是编译器提供了一个路子给你,

然而有些人确实可能会说,不相信编译器,

嗯,对的,我也不信。

但是,毫无疑问,大家虽然不信但是都在用。

嘴上那样说,身体还是很诚实的。

以上,权当抛砖引玉。

独乐乐不如一起玩乐。

若有其他相关问题或者需求也可以邮件联系俺探讨。

邮箱地址是: 
gaozhihan@vip.qq.com