[英]How to convert memcpy to memcpy_s?
I am just a beginner in C. I have written a code in C with a lot of memcpy functions. 我只是C的初学者。我用C编写了一个带有大量memcpy函数的代码。 I want to convert the memcpy statements to memcpy_s.. I don't quite get the syntax to do this.
我想将memcpy语句转换为memcpy_s ..我不太明白这样做的语法。
This is my code snippet: 这是我的代码片段:
signed char buffer[MAX];
unsigned char len;
const char *scenario = ConvertMap[identity];
len = strlen(scenario);
memcpy((void*)&buffer[0],scenario,len);
How do I convert the last line to memcpy_s? 如何将最后一行转换为memcpy_s? Is it like:
是这样的:
memcpy_s((void*)&buffer[0], sizeof(buffer), scenario, len)
? memcpy_s((void*)&buffer[0], sizeof(buffer), scenario, len)
?
The code could be: 代码可以是:
memcpy_s(buffer, sizeof(buffer), scenario, len)
although maybe you would want to use len+1
if you intend to copy a null-terminated string. 虽然如果你打算复制一个以null结尾的字符串,你可能想要使用
len+1
。 (But in that case, use strcpy_s
). (但在这种情况下,请使用
strcpy_s
)。
Also you should allow for the error case: either design the rest of your code to behave properly if the memcpy failed (buffer will be nulled out), or check the return value and take action on failure. 此外,您应该考虑错误情况:如果memcpy失败(缓冲区将被清空),请设计其余代码以使其正常运行,或检查返回值并在失败时采取措施。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.