diff between strcpy() and memcpy()

N

naren

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
 
S

slebetman

naren said:
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

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.
 
J

Jordan Abel

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

memcpy is not for strings. It's probably very rare that strlen+memcpy is
faster than strcpy.
 
K

Keith Thompson

naren said:
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?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top