Aspects


vs


Memcpy

Memcpy() is a C standard library function used for copying a block of memory bytes from one place to another.

Strncpy

A C standard library function: strncpy is used to copy a maximum of n characters of non-overlapping, fixed-width or null-terminated strings

Others

Quality Example
Worse

"If you compute the length of the string for unrelated reasons or have the length of the string from other resources it s unclear to me whether memcpy is better or worse than strncpy"

from question "Is it bad practice to use memcpy over strncpy or similar?"

Slower

"But for the example you give it doesn t matter - if it s going to fail it will be in the initial strlen so strncpy doesn t buy you anything in terms of safety and presumbly strncpy is slower as it has to both check bounds and for nul and any difference between memcpy and strcpy isn t worth changing code for speculatively"

from question "C strcpy() - evil?"

Much faster

"Even if underlying implementation is not so different memcpy is much faster because it does not have to check what it s copying strncpy will stop when it ll copy the end of string character null"

from question "Function selection for copying string in C++, with focus on performance"

Faster

"Memcpy will always be faster than strncpy for any real world situation even in the corner case i spoke before look page_copy_fwd_maybe"

from question "Function selection for copying string in C++, with focus on performance"

Version not more

"The memcpy version is not more complex or more dangerous than the strncpy version"

from question "Creating C substrings: looping with assignment operator VS strncopy, which is better?"

Probably faster

"Which is more efficient is up for debate but based on cpu bulk instructions which can copy an entire block of memory in one instruction memcpy is probably faster as strncpy would check each copied byte for a nul character"

from question "Is it bad practice to use memcpy over strncpy or similar?"

Back to Home
Data comes from Stack Exchange with CC-BY-SA-3.0