在 C 中如何使用以及何时使用 memmove 比较好?
我对memmove()的使用有两个疑问:
- 什么时候最好使用这个函数而不是使用另一个函数(即创建自己的函数)?我不确定我是否理解正确。
- 该函数的签名为void *memmove(void *dest, const void *src, size_t n)。如果我有一个简单的数组arr[N],我如何将它放入被调用的函数中? arr[N] 还是 &arr[N]?区别在于数组是用初始大小声明的还是像指针一样声明的?我有这个疑问,因为我看到很多同时使用两者的例子。
我希望我能很好地解释我的疑虑。
编辑:我必须从数组中删除一个元素,然后我想将已删除元素的以下元素移到左侧。
I have two doubts about the use of memmove():
- When is it preferable to use this function instead of using another function (i.e. a created own function)? I’m not sure I have understood properly.
- The signature of the function is void *memmove(void *dest, const void *src, size_t n). If I have a simple array arr[N], how can I put it into the called function? arr[N] or &arr[N]? The difference is if the array is declared with an initial size or like a pointer? I have this doubt because I saw many examples where is used both.
I hope I explained my doubts in a good way.
edit: I have to delete an element from the array, and then I want to shift the following elements of the deleted one on the left.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
memmove
可能会更快,但它可能永远不会比您自己的复制数据函数慢(它通常以精心设计的程序集进行编码,以便在当前架构上以最有效的方式移动内容);arr
就足够了(并且,作为长度参数,您应该执行sizeof( *arr)*N
其中 N 是要复制的元素数量)。顺便说一句,如果源和目标以及副本不重叠,
memcpy
可能会更快。(
sizeof(*arr)
表示“获取数组元素的大小”)memmove
may be faster but it probably will never be slower than your own function for copying data around (it's usually coded in carefully crafted assembly to move stuff around in the most efficient way possible on the current architecture);arr
will suffice (and, as the length parameter, you should dosizeof(*arr)*N
where N is the number of elements to copy).By the way, if source and destination and the copy are nonoverlapping
memcpy
may be faster.(
sizeof(*arr)
means "get the size of an element of the array")memmove
与memcpy
类似,只是目标数组和源数组可以重叠。使用memcpy,您可以保证区域不重叠,这允许实现执行一些额外的优化。因此,
memcpy
可能比memmove
更快。memmove
函数采用void *
目标参数和const void *
源参数。这意味着您可以使用数组类型的目标和源参数调用该函数,因为它们将转换为指针类型。由于您可以将任何非限定对象指针类型分配给void *
或const void *
,因此在调用函数时不需要任何强制转换。现在,使用
memcpy
或memmove
通常比编写自己的函数更好。例如,在 glibc 中,根据 MCU 指令集和要复制的大小,编译器可以使用memcpy
的一些快速内联汇编版本来替换memcpy
。memmove
is likememcpy
except the destination and source array can overlap.With
memcpy
you promise that the regions are not overlapping which allows the implementation to perform some additional optimizations. Somemcpy
can be faster thanmemmove
.The
memmove
function takes avoid *
destination argument and aconst void *
source argument. It means you can call the function with destination and source argument of array type because they will be converted to pointer types. And as you can assign any non-qualified object pointer types tovoid *
orconst void *
, you won't need any cast when calling the function.Now it's usually better to use
memcpy
ormemmove
than to code your own function. In glibc for example, depending on the MCU instruction set and the size to copy,memcpy
can be replaced by the compiler with some fast inline assembly versions ofmemcpy
.它比“创建自己的函数”更快。
只是
arr
?It's faster than a "created own function".
Just
arr
?