This is the 29th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.
String copying can be done using sprintf, strcpy, and memcpy functions, which differ in their implementation and object.
strcpy
The: function copies a string from a source string to a destination string.sprintf
This function converts (a string or primitive data type) to a string. String copying is also possible if the source object is a string and the %s format is specified.memcpy
The memory copy function is used to copy the contents of one memory block to another. A memory block is defined by its initial address and memory length information, and the object is operable. In view of thememcpy
The nature of isometric copies of functions and the physical implications of data types,memcp
Y functions are usually limited to copying data or objects of the same type, which of course includes copying strings and primitive data types.
For string copy, the above three functions can be implemented, but their implementation efficiency and convenience are different:
strcpy
Is undoubtedly the most suitable choice: efficient and easy to call.sprintf
It is cumbersome and inefficient to specify additional formatters and perform formatting transformations.memcpy
Although efficient, but need to provide additional copy of the memory length of the parameter, easy to error and inconvenient to use; And if the length is specified too large (the optimal length is the source string length +1), performance can degrade. Actually,strcpy
Functions are usually called internallymemcpy
Function or assembly directly implemented to achieve efficient purposes. Therefore, usememcpy
和strcpy
Copying strings should make no significant difference in performance.- For copying non-string data,
strcpy
andsprintf
There’s usually nothing you can do about it, but yeahmemcpy
It had no effect. However, for primitive data types, it does workmemcpy
Copy, because there are assignment operators that can easily and efficiently copy data of the same or compatible typememcpy
Hardly ever used.memcpy
The advantage is that it is used to implement (mostly internal) copies of structures or arrays, which are intended to be efficient, easy to use, or both.
Sprintf, strCPy,memcpy use precautions
strcpy
Is a string copy function whose function prototype isstrcpy(char dst, const char src);
- will
src
Copy the beginning of a string todst
The beginning of the memory, the end of the symbol is’ \0 ‘, because the length of the copy is not controlled by our own, so this string copy is prone to error. - Functions that copy strings are
memcpy
, this is a memory copy function, its function prototype ismemcpy(char dst, const char src, unsigned int len);
The length islen
A segment of memory fromsrc
Copy todst
The length of this function is controllable. But there will be memory read and write errors. (For example, len is longer than the space to be copied or the destination space) sprintf
Is a formatting function. To format a piece of data into a string buffer in a specific format.sprintf
The length of the formatted function is uncontrollable, and the formatted string may exceed the size of the buffer, causing overflow.