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.

Share:
10,070
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    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
    nhed about 3 years
    or strncpy_s?