C++/cpp doubt
Expert: Ralph McArdell - 9/8/2008
QuestionWhat is the difference between the strcpy and memcpy
AnswerWell this is more of a C question than a C++ question.
As an aside note that cpp does not necessarily mean C++ - cpp is also a program on UN*X and similar systems that handles C/C++ preprocessing (i.e. cpp is short for C Pre-Processor). Just so you know.
I say this is more of a C question because strcpy and memcpy are part of the C++ standard library that C++ inherited from C.
Both strcpy and memcpy handle copying chunks of memory specified by pointers from one place to another. The difference is how they do it. The strcpy function is intended for char arrays that represent C-style strings - i.e. zero terminated arrays of char, hence the str prefix. However memcpy is intended for any contiguous chunks of memory. This becomes obvious from their respective signatures:
char * strcpy( char * dest, char const * src );
void * memcpy( void * dest, void const * src, size_t n );
Both copy from the src to dest. However, note that whereas memcpy has a third parameter that specifies how many chars to copy strcpy does not - it continues copying up to and including the first zero valued char from src. Note also that whereas strcpy specifies char arrays (char pointers) and returns a pointer to the copied C-string (i.e. a pointer to src), memcpy specifies void * values and returns a pointer to the dest void pointer - so can be passed pointers to anything, but treats them as sequences of char (i.e. char arrays).
There is a variation of strcpy called strncpy that copies n characters from src to dest and its signature looks similar that of memcpy:
char * strncpy( char * dest, char const * src, size_t n );
However, strncpy is _still_ for use with zero-terminated arrays of char (i.e. C-strings), so still specifies src and dest in terms of pointers to (zero terminated) char arrays. It copies at most n characters from dest to src, and like strcpy it returns a pointer to src. If src has fewer than n characters then strncpy pads characters at dest with '\0' character values.
Below are the man pages from a handily Linux session I have to hand for strcpy, strncpy and, following these, memcpy et al.
man strcpy
Gives the following description:
"NAME
strcpy, strncpy - copy a string
SYNOPSIS
#include <string.h>
char *strcpy(char *dest, const char *src);
char *strncpy(char *dest, const char *src, size_t n);
DESCRIPTION
The strcpy() function copies the string pointed to by src, including the terminating null byte (’\0’), to the buffer pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy.
The strncpy() function is similar, except that at most n bytes of src are copied.
Warning: If there is no null byte among the first n bytes of src, the string placed in dest
will not be null terminated.
If the length of src is less than n, strncpy() pads the remainder of dest with null bytes.
A simple implementation of strncpy() might be:
char*
strncpy(char *dest, const char *src, size_t n){
size_t i;
for (i = 0 ; i < n && src[i] != ’\0’ ; i++)
dest[i] = src[i];
for ( ; i < n ; i++)
dest[i] = ’\0’;
return dest;
}
RETURN VALUE
The strcpy() and strncpy() functions return a pointer to the destination string dest."
And:
man memcpy
Gave this description:
"NAME
memcpy - copy memory area
SYNOPSIS
#include <string.h>
void *memcpy(void *dest, const void *src, size_t n);
DESCRIPTION
The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas should not overlap. Use memmove(3) if the memory areas do overlap.
RETURN VALUE
The memcpy() function returns a pointer to dest."
I should note and heed the small print and warnings in these descriptions if I were you, i.e. that strncpy does not necessarily terminate the C-string pointed to by dest and that the memory areas specified by src and dest for memcpy must be separate and not overlap (use memmove for such cases). Oh, and for ISO/ANSI standard C++ we include <cstring> rather than <string.h>.
Note that if you do not have a system with descriptions of such standard library functions then you can find them easily enough online using a search engine - try entering the name of the function you are interested in.
Most Linux and UN*X systems should have the manual (man) pages installed for such functions and if not then you may be able to install them - e.g. by selecting the appropriate packages to install on a Linux system from a package manager application..
For Microsoft compilers, check the documentation that comes with the Visual C++ product (a version of the MSDN library for local use is usually included or available for 'recent' releases) - or check out the MSDN library online at
http://msdn2.microsoft.com/
Please refer to documentation for such standard functions before asking similar questions in the future as you will probably find the information you are looking for by reading carefully such descriptions, and you will find it a lot quicker! Using me as a reference manual is something of a waste of both our time <g>. However, if after carefully reading such descriptions you still have queries then please ask a question - however be aware that I will probably start off by reviewing similar if not the same descriptions for such standard functions...
Hope this help.