Windows下memcpy_s如何在Linux下使用

Windows下代码如下

memcpy_s(pLine->ppBuf[i], m_ColorLineByte, pIn + nOffset, m_ColorLineByte);

方案 1:使用标准 memcpy + 手动检查(最通用)

// 检查参数有效性

if (pLine->ppBuf[i] == nullptr || pIn == nullptr ||

m_ColorLineByte == 0 || nOffset < 0) {

// 错误处理

return; // 或抛出异常

}

// 执行拷贝

memcpy(pLine->ppBuf[i], pIn + nOffset, m_ColorLineByte);

方案 2:使用 C11 的 memcpy_s(需编译器支持)

#if STDC_VERSION >= 201112L || STDC_LIB_EXT1

errno_t err = memcpy_s(pLine->ppBuf[i], m_ColorLineByte,

pIn + nOffset, m_ColorLineByte);

if (err != 0) {

// 错误处理

}

#else

// 回退到方案1

#endif

方案 3:使用 C++ 安全拷贝(推荐)

#include <algorithm> // for std::copy_n

try {

std::copy_n(pIn + nOffset, m_ColorLineByte, pLine->ppBuf[i]);

} catch (...) {

// 异常处理

}

相关推荐
blasit36 分钟前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_1 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
chlk1231 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑1 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件1 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
不想写代码的星星1 天前
虚函数表:C++ 多态背后的那个男人
c++
深紫色的三北六号2 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash2 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI2 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行3 天前
Linux和window共享文件夹
linux