问题描述
使用的memcpy()
当源和目的地的重叠可能会导致不确定的行为 - 在这种情况下只有 memmove与()
可以使用。
Using memcpy()
when source and destination overlap can lead to undefined behaviour - in those cases only memmove()
can be used.
但如果我知道肯定缓冲区不重叠 - 有没有理由使用专门的memcpy()
或专的memmove()
?我应该使用,为什么?
But what if I know for sure buffers don't overlap - is there a reason to use specifically memcpy()
or specifically memmove()
? Which should I use and why?
推荐答案
假设一个健全的库实现者,的memcpy
将永远在至少一样快 memmove与
。然而,在大多数平台上的差别将是最小的,而且在许多平台的memcpy
只是一个别名 memmove与
来支持传统code,它(错误地)调用的memcpy
上重叠的缓冲区。
Assuming a sane library implementor, memcpy
will always be at least as fast as memmove
. However, on most platforms the difference will be minimal, and on many platforms memcpy
is just an alias for memmove
to support legacy code that (incorrectly) calls memcpy
on overlapping buffers.
两者的memcpy
和 memmove与
应写入充分利用该平台上最快的载入和存储的。
Both memcpy
and memmove
should be written to take advantage of the fastest loads and stores available on the platform.
要回答你的问题:你应该使用一个是语义正确的。如果你能保证缓冲区不重叠,你应该使用的memcpy
。如果你不能保证缓冲区不重叠,你应该使用 memmove与
。
To answer your question: you should use the one that is semantically correct. If you can guarantee that the buffers do not overlap, you should use memcpy
. If you cannot guarantee that the buffers don't overlap, you should use memmove
.
这篇关于使用哪一个 - memmove与()或memcpy的() - 当缓冲区不重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!