Difference between std::strcpy and std::memcpy

Hi everybody,

You can copy a C-style null-terminated string using std::strcpy easily. You can also copy it using std::memcpy. The latter needs one additional argument, namely the amount of memory to copy.

Except for this, when copying a C-style string, is there a difference between the two functions? I personally think so, but perhaps I miss something...

See ya, Bilderbikkel

[code]
#include
#include
#include

int main()
{
char myArray[4] = {''};
{
//Using std::strcpy
std::strcpy(myArray,"yes");
assert(std::strcmp(myArray,"yes")==0);
std::printf("myArray: '%s'
",myArray);
}
{
//Using std::strcpy
std::memcpy(myArray,"no",(std::size_t)sizeof(char) * 3);
assert(std::strcmp(myArray,"no")==0);
std::printf("myArray: '%s'
",myArray);
}
}
[/code]

Comments

  • : Hi everybody,
    :
    : You can copy a C-style null-terminated string using std::strcpy
    : easily. You can also copy it using std::memcpy. The latter needs one
    : additional argument, namely the amount of memory to copy.
    :
    : Except for this, when copying a C-style string, is there a
    : difference between the two functions? I personally think so, but
    : perhaps I miss something...
    :
    : See ya, Bilderbikkel
    :
    : [code]:
    : #include
    : #include
    : #include
    :
    : int main()
    : {
    : char myArray[4] = {''};
    : {
    : //Using std::strcpy
    : std::strcpy(myArray,"yes");
    : assert(std::strcmp(myArray,"yes")==0);
    : std::printf("myArray: '%s'
    ",myArray);
    : }
    : {
    : //Using std::strcpy
    : std::memcpy(myArray,"no",(std::size_t)sizeof(char) * 3);
    : assert(std::strcmp(myArray,"no")==0);
    : std::printf("myArray: '%s'
    ",myArray);
    : }
    : }
    : [/code]:
    strcpy is meant to copy only null-terminated strings. It is probably implemented to copy every byte until it encounters a #0.
    memcpy can copy any memory location. It is not bound by a null-terminated string. Since memcpy cannot determine the size of the data to be copied, it needs the programmer to provide that information.
  • : : Hi everybody,
    : :
    : : You can copy a C-style null-terminated string using std::strcpy
    : : easily. You can also copy it using std::memcpy. The latter needs one
    : : additional argument, namely the amount of memory to copy.
    : :
    : : Except for this, when copying a C-style string, is there a
    : : difference between the two functions? I personally think so, but
    : : perhaps I miss something...
    : :
    : : See ya, Bilderbikkel
    : :
    : : [code]: :
    : : #include
    : : #include
    : : #include
    : :
    : : int main()
    : : {
    : : char myArray[4] = {''};
    : : {
    : : //Using std::strcpy
    : : std::strcpy(myArray,"yes");
    : : assert(std::strcmp(myArray,"yes")==0);
    : : std::printf("myArray: '%s'
    ",myArray);
    : : }
    : : {
    : : //Using std::strcpy
    : : std::memcpy(myArray,"no",(std::size_t)sizeof(char) * 3);
    : : assert(std::strcmp(myArray,"no")==0);
    : : std::printf("myArray: '%s'
    ",myArray);
    : : }
    : : }
    : : [/code]: :
    : strcpy is meant to copy only null-terminated strings. It is probably
    : implemented to copy every byte until it encounters a #0.
    : memcpy can copy any memory location. It is not bound by a
    : null-terminated string. Since memcpy cannot determine the size of
    : the data to be copied, it needs the programmer to provide that
    : information.


    That is correct. Note that there is also a function called strncpy() which copies n bytes. It is almost identical to memcpy(), with the difference that it adds null termination at the end of the target string.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories