How to use and when is good use memmove in C?

倖福魔咒の 提交于 2019-12-05 14:56:19

问题


I have two doubt about use of memmove():

  • When is preferable use this function instead of use 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 example 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.


回答1:


  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")




回答2:


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.




回答3:


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?



来源:https://stackoverflow.com/questions/9041787/how-to-use-and-when-is-good-use-memmove-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!