在 C 中如何使用以及何时使用 memmove 比较好?

发布于 2024-12-29 17:14:41 字数 363 浏览 0 评论 0原文

我对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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

平定天下 2025-01-05 17:14:41
  1. memmove 可能会更快,但它可能永远不会比您自己的复制数据函数慢(它通常以精心设计的程序集进行编码,以便在当前架构上以最有效的方式移动内容);
  2. 这取决于您想对该数组执行什么操作...如果您想将其内容复制到另一个数组 arr 就足够了(并且,作为长度参数,您应该执行 sizeof( *arr)*N 其中 N 是要复制的元素数量)。

顺便说一句,如果源和目标以及副本不重叠,memcpy 可能会更快。

我想从数组中删除一个元素,并将同一数组的元素左移。

int arr[N];
/* ... */
/* Let's say you want to remove the element i (error checking on i omitted) */
memmove(arr+i, arr+i+1, (N-i-1)*sizeof(*arr));
/* or, if you prefer array indexing over pointer arithmetics: */
memmove(&arr[i], &arr[i+1], (N-i-1)*sizeof(*arr));

sizeof(*arr) 表示“获取数组元素的大小”)

  1. 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);
  2. it depends on what you want to do with that array... if you want to copy its content to another array arr will suffice (and, as the length parameter, you should do sizeof(*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.

I want to delete an element from the array and shift left the element of the same array.

int arr[N];
/* ... */
/* Let's say you want to remove the element i (error checking on i omitted) */
memmove(arr+i, arr+i+1, (N-i-1)*sizeof(*arr));
/* or, if you prefer array indexing over pointer arithmetics: */
memmove(&arr[i], &arr[i+1], (N-i-1)*sizeof(*arr));

(sizeof(*arr) means "get the size of an element of the array")

情话墙 2025-01-05 17:14:41

memmovememcpy 类似,只是目标数组和源数组可以重叠。

使用memcpy,您可以保证区域不重叠,这允许实现执行一些额外的优化。因此,memcpy 可能比 memmove 更快。

memmove 函数采用 void * 目标参数和 const void * 源参数。这意味着您可以使用数组类型的目标和源参数调用该函数,因为它们将转换为指针类型。由于您可以将任何非限定对象指针类型分配给 void *const void *,因此在调用函数时不需要任何强制转换。

char src[1024] = {0};
char dst[1024];

memmove(dst, src, sizeof dst);
/* src and dst don't overlap you should choose memcpy instead */
memcpy(dst, src, sizeof dst);

现在,使用 memcpymemmove 通常比编写自己的函数更好。例如,在 glibc 中,根据 MCU 指令集和要复制的大小,编译器可以使用 memcpy 的一些快速内联汇编版本来替换 memcpy

memmove is like memcpy 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. So memcpy can be faster than memmove.

The memmove function takes a void * destination argument and a const 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 to void * or const void *, you won't need any cast when calling the function.

char src[1024] = {0};
char dst[1024];

memmove(dst, src, sizeof dst);
/* src and dst don't overlap you should choose memcpy instead */
memcpy(dst, src, sizeof dst);

Now it's usually better to use memcpy or memmove 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 of memcpy.

鲜肉鲜肉永远不皱 2025-01-05 17:14:41

什么时候最好使用这个函数而不是使用另一个函数(即创建自己的函数)

它比“创建自己的函数”更快。

该函数的签名为 void *memmove(void *dest, const void *src, size_t n)。如果我有一个简单的数组 arr[N],如何将其放入被调用的函数中? arr[N] 或 &arr[N]

只是 arr

When is preferable use this function instead of use another function (i.e. a created own function)

It's faster than a "created own function".

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]

Just arr?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文