How to convert memcpy to memcpy_s?
10,070
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. (But in that case, use 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.
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on June 04, 2022Comments
-
Admin about 2 years
I am just a beginner in C. I have written a code in C with a lot of memcpy functions. I want to convert the memcpy statements to memcpy_s.. I don't quite get the syntax to do this.
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? Is it like:
memcpy_s((void*)&buffer[0], sizeof(buffer), scenario, len)
? -
nhed about 3 yearsor
strncpy_s
?