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. 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) ?

14

1 Answer

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.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like