我在努力理解memcpy()
和memmove()
,我读过的文章说memcpy()
不考虑重叠的源和目标,而memmove()
会。
但是,当我在重叠内存块上执行这两个函数时,它们的结果都是相同的。例如,以下面的MSDN示例为例:
// crt_memcpy.c
// Illustrate overlapping copy: memmove always handles it correctly; memcpy may handle
// it correctly.
#include <memory.h>
#include <string.h>
#include <stdio.h>
char str1[7] = "aabbcc";
int main( void )
{
printf( "The string: %s\n", str1 );
memcpy( str1 + 2, str1, 4 );
printf( "New string: %s\n", str1 );
strcpy_s( str1, sizeof(str1), "aabbcc" ); // reset string
printf( "The string: %s\n", str1 );
memmove( str1 + 2, str1, 4 );
printf( "New string: %s\n", str1 );
}
The string: aabbcc
New string: aaaabb
The string: aabbcc
New string: aaaabb
发布于 2018-03-24 16:16:06
我并不惊讶你的例子没有表现出奇怪的行为。试试复制str1
,str1+2
然后看看会发生什么。
一般来说,memcpy以简单(但快速)的方式实现。简单来说,它只是循环遍历数据(按顺序),从一个位置复制到另一个位置。这可能会导致源在读取时被覆盖。
Memmove做了更多的工作来确保它正确处理重叠。
https://stackoverflow.com/questions/-100003684
复制相似问题