简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM