15

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.

Kyrol
  • 3,475
  • 7
  • 34
  • 46
  • 2
    Relevant. http://stackoverflow.com/questions/1960991/which-one-to-use-memmove-or-memcpy-when-buffers-dont-overlap – shadyabhi Jan 28 '12 at 00:18

3 Answers3

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

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • I want to delete an element from the array and shift left the element of the same array. – Kyrol Jan 28 '12 at 00:26
  • 2
    Note only might `memcpy` be faster for non-overlapping data; using `memcpy` also serves to document that you're copying between distinct objects rather than shifting elements within a single array object. – R.. GitHub STOP HELPING ICE Jan 28 '12 at 00:38
  • Ok thanks. I understand the difference. Grazie mille from Italy to. ;) – Kyrol Jan 28 '12 at 00:38
  • 1
    @Kyrol: you're welcome; tra l'altro, tanta stima per l'avatar dei Pink Floyd. :) – Matteo Italia Jan 28 '12 at 00:41
  • @Kyrol: That certainly sounds like a job for `memmove()`. – Keith Thompson Jan 28 '12 at 00:45
  • When I originally wrote this functionality in my program (without consulting anything but the memmove docs), I was coding a resizing array of pointers. Turns out, I forgot the `&` in front of the `myarray[i]` when using the index method. Needless say, it did not end well. :] – flagoworld Jun 19 '13 at 01:55
8

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.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • and if src and dst are the same array?? because the doubt is if I have to delete an element and shift left the others elements after the deleted one. – Kyrol Jan 28 '12 at 00:33
  • @Kyrol: still with `char src[1024] = {0}` example. `memcpy(src, src + 512, 512);` same object but regions don't overlap: that's ok. But `memcpy(src + 256, src + 512, 512);` regions overlap, that's not ok, use `memmove` instead. – ouah Jan 28 '12 at 00:38
  • its worth noting that memcpy is only faster than memmove by one if statement – Seth Carnegie Jan 28 '12 at 00:49
  • @SethCarnegie yes, if the C version of `memcpy` is called. Some fast assembly builtins of `memmove` cannot be achieved with the same MCU instructions as `memcpy`. – ouah Jan 28 '12 at 00:55
  • @SethCarnegie: If an implementation guarantees that an expression like `(p>q)` will never do anything other than yielding 1 or 0, even if `p` and `q` point to unrelated memory areas, then it could implement `memmove()` to require only one extra `if`. Given `char *p=malloc(16), q[16];` the expression `(p>q)` could do anything (i.e. Undefined Behavior). Even though, for any valid source and destination pointers, either an ascending loop would work or a descending loop would work, there's no portable way to determine which one would be safe. – supercat Dec 06 '14 at 19:29
0

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?

James M
  • 18,506
  • 3
  • 48
  • 56