Connecting Tech Pros Worldwide Forums | Help | Site Map

diff between strcpy() and memcpy()

naren
Guest
 
Posts: n/a
#1: Mar 15 '06
Iam not getting the correct pros and cons of the strcpy() and memcpy()
some where i read for short strings strcpy is faster and for large
strings memcpy is faster..
in strcpy() there is a single pass over the data...but in memcpy()
there are 2 passes..one is for strlen() and another is for copying..
but how memcpy will be faster for large strings?

Can anybody please clear my doubt


slebetman@yahoo.com
Guest
 
Posts: n/a
#2: Mar 15 '06

re: diff between strcpy() and memcpy()


naren wrote:[color=blue]
> Iam not getting the correct pros and cons of the strcpy() and memcpy()
> some where i read for short strings strcpy is faster and for large
> strings memcpy is faster..
> in strcpy() there is a single pass over the data...but in memcpy()
> there are 2 passes..one is for strlen() and another is for copying..
> but how memcpy will be faster for large strings?
>
> Can anybody please clear my doubt[/color]

The difference is in the terminating conditions. Did you read the
manual or at least the prototype of the functions?

char *strcpy(char *dest, const char *src);
void *memcpy(void *dest, const void *src, size_t n);

See that last argument in memcpy? That's the difference. strcpy() just
copies until it sees a null terminator while you need to tell memcpy
how long the data you want to copy actually is. You can't simply
substitute one for the other.

Jordan Abel
Guest
 
Posts: n/a
#3: Mar 15 '06

re: diff between strcpy() and memcpy()


On 2006-03-15, naren <narendar.anumala@gmail.com> wrote:[color=blue]
> Iam not getting the correct pros and cons of the strcpy() and memcpy()
> some where i read for short strings strcpy is faster and for large
> strings memcpy is faster..
> in strcpy() there is a single pass over the data...but in memcpy()
> there are 2 passes..one is for strlen() and another is for copying..
> but how memcpy will be faster for large strings?
>
> Can anybody please clear my doubt[/color]

memcpy is not for strings. It's probably very rare that strlen+memcpy is
faster than strcpy.
Keith Thompson
Guest
 
Posts: n/a
#4: Mar 15 '06

re: diff between strcpy() and memcpy()


"naren" <narendar.anumala@gmail.com> writes:[color=blue]
> Iam not getting the correct pros and cons of the strcpy() and memcpy()
> some where i read for short strings strcpy is faster and for large
> strings memcpy is faster..
> in strcpy() there is a single pass over the data...but in memcpy()
> there are 2 passes..one is for strlen() and another is for copying..
> but how memcpy will be faster for large strings?[/color]

memcpy() and strcpy() are functionally different. Use memcpy() if you
already know how many bytes you want to copy. Use strcpy() if you
want to copy a string (i.e., up to the first '\0' character).

If you've already computed the length of your string (with strlen()),
you have a choice -- but it probably doesn't make much difference.
There's no obvious reason why one or the other should be faster,
and it's likely to vary from one system to another.

Use whichever is correct. If they're both correct, use whichever one
is clearer (likely strcpy() if you're dealing with strings). If *and
only if* performance is a problem consider choosing on the basis of
speed, and only after measuring the actual performance.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Closed Thread