memmove(3)

copy memory area

Section 3 manpages-dev bookworm source

Summary

memmove() copies n bytes from the memory area src to the memory area dest and returns a pointer to dest. Unlike memcpy(), it is safe to use when the source and destination regions overlap, because it behaves as if the bytes were first copied to a temporary buffer. Use it whenever you shift or move data within a single buffer or whenever overlap between src and dest is possible.

Tip: Reach for memmove instead of memcpy any time the source and destination ranges might overlap; memcpy gives undefined behavior on overlapping regions, while memmove is guaranteed correct.

Description

memmove

NAME

memmove - copy memory area

LIBRARY

Standard C library (libc, -lc)

SYNOPSIS

#include <string.h>

void *memmove(void dest[.n], const void src[.n], size_t n);

DESCRIPTION

The memmove() function copies n bytes from memory area src to memory area dest. The memory areas may overlap: copying takes place as though the bytes in src are first copied into a temporary array that does not overlap src or dest, and the bytes are then copied from the temporary array to dest.

RETURN VALUE

The memmove() function returns a pointer to dest.

ATTRIBUTES

For an explanation of the terms used in this section, see attributes(7).

Image grohtml-41900-1.png

STANDARDS

POSIX.1-2001, POSIX.1-2008, C99, SVr4, 4.3BSD.

SEE ALSO

bcopy(3), bstring(3), memccpy(3), memcpy(3), strcpy(3), strncpy(3), wmemmove(3)

Examples

#include <string.h>
memmove(dest, src, n);  /* copy n bytes, overlap-safe */

Basic copy of n bytes from src to dest.

/* shift array elements left by one, regions overlap */
memmove(&arr[0], &arr[1], (count - 1) * sizeof(arr[0]));

Remove the first element by moving the rest down within the same buffer.

char *p = memmove(buf, buf + 2, len - 2);
/* p == buf */

memmove returns a pointer to dest, so it can be used inline.