memmove() Function

The memmove() function in C moves a block of memory from one location to another. It copies exactly num bytes from the source address to the destination address. Unlike memcpy(), memmove() ensures that overlapping memory regions are handled correctly by using an intermediate buffer.


Syntax of memmove()

</>
Copy
void *memmove(void *destination, const void *source, size_t num);

Parameters

ParameterDescription
destinationPointer to the destination memory block where data is to be copied.
sourcePointer to the source memory block from which data is copied.
numNumber of bytes to copy.

Return Value

The function returns a pointer to the destination memory block.

Exceptions

memmove() does not throw exceptions but may cause undefined behavior if:

  • Either destination or source is a null pointer.
  • The memory regions do not have at least num bytes allocated.

Examples for memmove()

Example 1: Copying a Non-Overlapping Memory Block

This example demonstrates how to use memmove() to copy data between two non-overlapping memory blocks:

Program

</>
Copy
#include <stdio.h>
#include <string.h>

int main() {
    char source[] = "Hello, World!";
    char destination[20];

    // Copying from source to destination
    memmove(destination, source, strlen(source) + 1);

    printf("Copied string: %s\n", destination);
    return 0;
}

Explanation:

  1. A character array source is initialized with the string "Hello, World!".
  2. A character array destination is declared with enough space to hold the copied string.
  3. The memmove() function copies the contents of source into destination, including the null terminator.
  4. The copied string is printed using printf().

Output:

Copied string: Hello, World!

Example 2: Handling Overlapping Memory Blocks

This example demonstrates how memmove() correctly handles overlapping memory regions:

Program

</>
Copy
#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "apple banana cherry";

    // Move substring within the same buffer
    memmove(str + 8, str, 8);

    printf("Modified string: %s\n", str);
    return 0;
}

Explanation:

  1. A character array str is initialized with the text "memmove function example".
  2. The memmove() function moves the first 8 characters to a new position starting at index 8 within the same buffer.
  3. The modified string is printed to demonstrate that the overlapping regions were handled correctly.

Output:

Modified string: apple baapple barry

Example 3: Copying a Struct Using memmove()

This example demonstrates how to use memmove() to copy a struct from one location to another:

Program

</>
Copy
#include <stdio.h>
#include <string.h>

typedef struct {
    int id;
    char name[20];
} Person;

int main() {
    Person p1 = {101, "Arjun"};
    Person p2;

    // Copying structure p1 to p2
    memmove(&p2, &p1, sizeof(Person));

    printf("Copied Person ID: %d, Name: %s\n", p2.id, p2.name);
    return 0;
}

Explanation:

  1. A struct Person is defined, containing an integer ID and a character array for the name.
  2. An instance p1 is initialized with values.
  3. The memmove() function copies p1 to p2.
  4. The copied values in p2 are printed to confirm successful copying.

Output:

Copied Person ID: 101, Name: Arjun