加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – Mac OS X相当于SecureZeroMemory / RtlSecureZer

发布时间:2020-12-16 03:35:21 所属栏目:百科 来源:网络整理
导读:是否有一个Mac OS X等效的 RtlSecureZeroMemory / SecureZeroMemory ,这是一个零内存块的功能,但是调用不会被编译器优化掉? 解决方法 写你自己的功能: void secure_zero(void *s,size_t n){ volatile char *p = s; while (n--) *p++ = 0;} 编辑:在评论中
是否有一个Mac OS X等效的 RtlSecureZeroMemory/ SecureZeroMemory,这是一个零内存块的功能,但是调用不会被编译器优化掉?

解决方法

写你自己的功能:
void secure_zero(void *s,size_t n)
{
    volatile char *p = s;

    while (n--) *p++ = 0;
}

编辑:在评论中的问题,为什么不memset?如果数组对象不被访问,则可以通过编译器优化掉memset函数调用.

请注意,C11添加(可选)功能memset_s和标准保证函数调用无法优化:

(C11,K.3.7.4.1p4) “[…] Unlike memset,any call to the memset_s function shall be evaluated strictly according to the rules of the abstract machine as described in (5.1.2.3). That is,any call to the memset_s function shall assume that the memory indicated by s and n may be accessible in the future and thus must contain the values indicated by c.”

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读