I've read the function headers, but I'm still not sure what exactly the difference is in terms of use cases.
Notice that memcpy is only slightly slower then memset . The operations a[j] += b[j] (where j goes over [0,LEN) ) should take three times longer than memcpy because it operates on three times as much data. However it's only about 2.5 as slow as memset .
memcpy() function in C/C++ The function memcpy() is used to copy a memory block from one location to another. One is source and another is destination pointed by the pointer. This is declared in “string. h” header file in C language.
strcpy () is meant for strings only whereas memcpy() is generic function to copy bytes from source to destination location. The strcpy ( ) function is designed to work exclusively with strings.
The memset() function sets the first count bytes of dest to the value c. The value of c is converted to an unsigned character.
memcpy()
copies from one place to another. memset()
just sets all pieces of memory to the same value.
Example:
memset(str, '*', 50);
The above line sets the first 50 characters of the string str to * (or whatever second argument of the memset).
memcpy(str2, str1, 50);
The above line copies the first 50 characters of str1 to str2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With