用C实现的memmove [英] memmove implementation in C
问题描述
有人能帮助我了解memmove与如何用C实现的我只有一个特殊情况吧?
Can some one help me to understand how memmove is implemented in C. I have only one special condition right ?
if((src<dst)&&((src+sz) > dst))
copy from the back
另外它依赖于堆栈增长方式?
Also does it depend on the way stack grows ?
推荐答案
在数学上,你不必担心是否重叠的。如果的src
小于 DST
,刚刚从高端副本。如果的src
比 DST
时,从一开始就只是复制。
Mathematically, you don't have to worry about whether they overlap at all. If src
is less than dst
, just copy from the end. If src
is greater than dst
, just copy from the beginning.
如果的src
和 DST
都是平等的,只是退出立竿见影。
If src
and dst
are equal, just exit straight away.
这是因为你的案件之一:
That's because your cases are one of:
1) <-----s-----> start at end of s
<-----d----->
2) <-----s-----> start at end of s
<-----d----->
3) <-----s-----> no action
<-----d----->
4) <-----s-----> start at beginning of s
<-----d----->
5) <-----s-----> start at beginning of s
<-----d----->
即使没有重叠,这将仍然正常工作,并简化您的条件。
Even if there's no overlap, that will still work fine, and simplify your conditions.
如果你有一个更有效的方式来复制比向前向后那么,是的,你应该检查重叠,以确保您尽可能使用更有效的方法。换句话说,上述更改选项1,从一开始复制。
If you have a more efficient way to copy forwards than backwards then, yes, you should check for overlap to ensure you're using the more efficient method if possible. In other words, change option 1 above to copy from the beginning.
然而,在这种情况下,你可能也发现它的速度更快复制两次,一次是从取值
来那么一些暂时性的内存从暂时存储器 D
但是这完全取决于你的各自的算法。
However, in that case, you may also find that it's faster to copy twice, once from s
to some temporary memory then from that temporary memory to d
but that depends entirely on your respective algorithms.
这篇关于用C实现的memmove的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!