I want to copy a null-terminated string to another location and want to know how long the copied string was. Efficiency is of utmost importance. There ist the strcpy
function which can achieve this, but it does not return how many characters are actually copied.
Of course, I could find this out by simply calling strlen
afterwards to detect the length of the copied string, but this would mean traversing the characters in the string again a second time, although strcpy
has to track how many characters it copies anyway. For performance reasons, I do not want such a second traversal.
I know that writing an own strcpy
with a simple char-by-char copy is easy, but I thought that the standard library might apply magic which makes strcpy
faster than a naive char-by-char implementation.
So, what is the best method to strcpy
and receive the number of copied characters without traversing the string again?
strcpy
is not complicated you could rite your own ... – Plasmolysisstd::string
s and not have to worry about this. – Trichomoniasisstrlen
on the source string; 2) Callmemcpy
. The idea is thatmemcpy
may be faster thanstrcpy
because it need not check for the terminating null, and knows the number of bytes to be copied in advance. – Genteel