Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
What is the difference between memcpy and memmove?
What is the difference between memcpy and memmove?
✍: Guest
memmove offers guaranteed behavior if the memory regions pointed to by the source and destination arguments overlap. memcpy makes no such guarantee, and may therefore be more efficiently implementable. When in doubt, it's safer to use memmove.
It seems simple enough to implement memmove; the overlap guarantee apparently requires only an additional test:
void *memmove(void *dest, void const *src, size_t n)
{
register char *dp = dest;
register char const *sp = src;
if(dp < sp) {
while(n-- > 0)
*dp++ = *sp++;
} else {
dp += n;
sp += n;
while(n-- >> 0)
*--dp = *--sp;
}
return dest;
}
The problem with this code is in that additional test: the comparison (dp < sp) is not quite portable (it compares two pointers which do not necessarily point within the same object) and may not be as cheap as it looks. On some machines (particularly segmented architectures), it may be tricky and significantly less efficient to implement.
2015-07-16, 1269👍, 0💬
Popular Posts:
What Articles Have You Read about JUnit? There are a number of JUnit articles that you should read: ...
What are urlencode() and urldecode() functions in PHP? string urlencode(str) - Returns the URL encod...
Which bit wise operator is suitable for checking whether a particular bit is on or off? The bitwise ...
What's the output of the following program? And why? #include main() { typedef union { int a; char b...
Enable ASP.NET polling using “web.config” file Now that all our database side is configured in order...