memcpy() 与 memmove()
我试图理解 memcpy()
< 之间的区别/a> 和 memmove()
,我读过的文字是 memcpy()
不处理重叠的源和目标,而 memmove()
则负责。
但是,当我在重叠的内存块上执行这两个函数时,它们都会给出相同的结果。例如,以 memmove()
帮助页面上的以下 MSDN 示例为例: -
有没有更好的示例来理解 memcpy
的缺点以及 memmove
的工作原理代码> 解决了吗?
// 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 );
}
输出:
memcpy():
The string: aabbcc
New string: aaaabb
memmove():
The string: aabbcc
New string: aaaabb
I am trying to understand the difference between memcpy()
and memmove()
, and I have read the text that memcpy()
doesn't take care of the overlapping source and destination whereas memmove()
does.
However, when I execute these two functions on overlapping memory blocks, they both give the same result. For instance, take the following MSDN example on the memmove()
help page:-
Is there a better example to understand the drawbacks of memcpy
and how memmove
solves it?
// 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 );
}
Output:
memcpy():
The string: aabbcc
New string: aaaabb
memmove():
The string: aabbcc
New string: aaaabb
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
对于您的示例没有表现出奇怪的行为,我并不完全感到惊讶。尝试将
str1
复制到str1+2
并看看会发生什么。 (实际上可能没有什么区别,取决于编译器/库。)通常,memcpy 以简单(但快速)的方式实现。简单地说,它只是循环数据(按顺序),从一个位置复制到另一个位置。这可能会导致源在读取时被覆盖。
memmove
做了更多工作来确保它正确处理重叠。编辑:(
不幸的是,我找不到合适的例子,但这些就可以了)。比较
memcpy
和memmove
实现此处显示。memcpy
只是循环,而memmove
执行测试以确定循环方向以避免损坏数据。这些实现相当简单。大多数高性能实现都更加复杂(涉及一次复制字大小的块而不是字节)。I'm not entirely surprised that your example exhibits no strange behaviour. Try copying
str1
tostr1+2
instead and see what happens then. (May not actually make a difference, depends on compiler/libraries.)In general,
memcpy
is implemented in a simple (but fast) manner. Simplistically, it just loops over the data (in order), copying from one location to the other. This can result in the source being overwritten while it's being read.memmove
does more work to ensure it handles the overlap correctly.EDIT:
(Unfortunately, I can't find decent examples, but these will do). Compare the
memcpy
andmemmove
implementations shown here.memcpy
just loops, whilememmove
performs a test to determine which direction to loop in to avoid corrupting the data. These implementations are rather simple. Most high-performance implementations are more complicated (involving copying word-size blocks at a time rather than bytes).memcpy
中的内存不能重叠,否则可能会出现未定义行为,而memmove
中的内存可以重叠。memcpy
的某些实现可能仍然适用于重叠输入,但您不能指望这种行为。但是,memmove 必须允许重叠输入。The memory in
memcpy
cannot overlap or you risk undefined behaviour, while the memory inmemmove
can overlap.Some implementations of
memcpy
might still work for overlapping inputs, but you cannot count on that behaviour. However,memmove
must allow for overlapping inputs.仅仅因为
memcpy
不必处理重叠区域,并不意味着它不能正确处理它们。具有重叠区域的调用会产生未定义的行为。未定义的行为可以在一个平台上完全按照您的预期工作;这并不意味着它是正确或有效的。Just because
memcpy
doesn't have to deal with overlapping regions, doesn't mean it doesn't deal with them correctly. The call with overlapping regions produces undefined behavior. Undefined behavior can work entirely as you expect on one platform; that doesn't mean it's correct or valid.memcpy 和 memove 都做类似的事情。
但要找出一个区别:
给出:
Both memcpy and memove do similar things.
But to sight out one difference:
gives:
您的演示没有因为“坏”编译器而暴露 memcpy 缺点,它在调试版本中对您有帮助。然而,发布版本为您提供相同的输出,但由于优化。
这里的寄存器%eax充当临时存储,它“优雅地”修复了重叠问题。
当复制 6 个字节(至少是其中的一部分)时,就会出现缺点。
输出:
看起来很奇怪,这也是由优化引起的。
这就是为什么我在尝试复制 2 个重叠的内存块时总是选择 memmove 的原因。
Your demo didn't expose memcpy drawbacks because of "bad" compiler, it does you a favor in Debug version. A release version, however, gives you the same output, but because of optimization.
The register
%eax
here plays as a temporary storage, which "elegantly" fixes overlap issue.The drawback emerges when copying 6 bytes, well, at least part of it.
Output:
Looks weird, it's caused by optimization, too.
This is why I always choose
memmove
when trying to copy 2 overlapped memory blocks.C11 标准草案
C11 N1570标准草案说:
7.24.2.1“memcpy函数”:
7.24.2.2“memmove 函数”:
因此,memcpy 上的任何重叠都会导致未定义的行为,并且任何事情都可能发生:坏的、什么都没有甚至是好的。不过,好的东西很少见:-)
memmove
然而,它清楚地表明一切都会发生,就好像使用了中间缓冲区一样,因此显然重叠是可以的。然而,C++
std::copy
更宽容,并且允许重叠:std::copy 是否处理重叠范围?C11 standard draft
The C11 N1570 standard draft says:
7.24.2.1 "The memcpy function":
7.24.2.2 "The memmove function":
Therefore, any overlap on
memcpy
leads to undefined behavior, and anything can happen: bad, nothing or even good. Good is rare though :-)memmove
however clearly says that everything happens as if an intermediate buffer is used, so clearly overlaps are OK.C++
std::copy
is more forgiving however, and allows overlaps: Does std::copy handle overlapping ranges?memcpy
和memmove
的区别在于在
memmove
中,将指定大小的源内存复制到 buffer 中,然后移动到目标。因此,如果内存重叠,不会产生任何副作用。对于
memcpy()
,源内存不占用额外的缓冲区。复制是直接在内存上完成的,因此当存在内存重叠时,我们会得到意想不到的结果。这些可以通过以下代码观察:
输出为:
The difference between
memcpy
andmemmove
is thatin
memmove
, the source memory of specified size is copied into buffer and then moved to destination. So if the memory is overlapping, there are no side effects.in case of
memcpy()
, there is no extra buffer taken for source memory. The copying is done directly on the memory so that when there is memory overlap, we get unexpected results.These can be observed by the following code:
Output is:
正如其他答案中已经指出的那样,memmove 比 memcpy 更复杂,因此它可以解决内存重叠问题。 memmove 的结果定义为将
src
复制到缓冲区中,然后将缓冲区复制到dst
中。这并不意味着实际实现使用任何缓冲区,但可能会执行一些指针算术。As already pointed out in other answers,
memmove
is more sophisticated thanmemcpy
such that it accounts for memory overlaps. The result of memmove is defined as if thesrc
was copied into a buffer and then buffer copied intodst
. This does NOT mean that the actual implementation uses any buffer, but probably does some pointer arithmetic.编译器可以优化memcpy,例如:
这个memcpy可以优化为:
x = *(int*)some_pointer;
compiler could optimize memcpy, for example:
This memcpy may be optimized as:
x = *(int*)some_pointer;
memcpy 的链接 http://clc-wiki.net/wiki/memcpy 中给出的代码似乎让我有点困惑,因为当我使用下面的示例实现它时,它没有给出相同的输出。
输出:
但是您现在可以理解为什么 memmove 会处理重叠问题。
The code given in the links http://clc-wiki.net/wiki/memcpy for memcpy seems to confuse me a bit, as it does not give the same output when I implemented it using the below example.
Output :
But you can now understand why memmove will take care of overlapping issue.
这是我写的一个例子,请参考。只有当dst所在区域位于src区域后面时,才会出现内存复制覆盖问题。
This is an example I wrote, please refer to it. The memory copy overwriting problem will only occur when the area where dst is located is behind the src area.
我尝试使用 eclipse 运行相同的程序,它显示了
memcpy
和memmove
之间的明显区别。memcpy()
不关心内存位置的重叠,这会导致数据损坏,而memmove()
会先将数据复制到临时变量,然后复制到实际内存中地点。尝试将数据从位置
str1
复制到str1+2
时,memcpy
的输出为“aaaaaa
”。问题是如何?memcpy()
将从左到右一次复制一个字节。如您的程序“aabbcc
”所示,然后所有复制将按如下方式进行:
aabbcc -> aaabcc
aaabcc -> aaaacc
aaaacc -> aaaaac
aaaaac -> aaaaaa
memmove()
会先将数据复制到临时变量,然后复制到实际内存位置。aabbcc(实际) -> aabbcc(temp)
aabbcc(temp) -> aaabcc(act)
aabbcc(temp) -> aaaacc(act)
aabbcc(temp) -> aaaabc(act)
aabbcc(temp) -> aaaabb(act)
输出为
memcpy
:aaaaaa
memmove
:aaaabb
I have tried to run same program using eclipse and it shows clear difference between
memcpy
andmemmove
.memcpy()
doesn't care about overlapping of memory location which results in corruption of data, whilememmove()
will copy data to temporary variable first and then copy into actual memory location.While trying to copy data from location
str1
tostr1+2
, output ofmemcpy
is "aaaaaa
". The question would be how?memcpy()
will copy one byte at a time from left to right. As shown in your program "aabbcc
" thenall copying will take place as below,
aabbcc -> aaabcc
aaabcc -> aaaacc
aaaacc -> aaaaac
aaaaac -> aaaaaa
memmove()
will copy data to temporary variable first and then copy to actual memory location.aabbcc(actual) -> aabbcc(temp)
aabbcc(temp) -> aaabcc(act)
aabbcc(temp) -> aaaacc(act)
aabbcc(temp) -> aaaabc(act)
aabbcc(temp) -> aaaabb(act)
Output is
memcpy
:aaaaaa
memmove
:aaaabb