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
Parameter | Description |
---|---|
destination | Pointer to the destination memory block where data is to be copied. |
source | Pointer to the source memory block from which data is copied. |
num | Number 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
orsource
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:
- A character array
source
is initialized with the string"Hello, World!"
. - A character array
destination
is declared with enough space to hold the copied string. - The
memmove()
function copies the contents ofsource
intodestination
, including the null terminator. - 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:
- A character array
str
is initialized with the text"memmove function example"
. - The
memmove()
function moves the first 8 characters to a new position starting at index 8 within the same buffer. - 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:
- A struct
Person
is defined, containing an integer ID and a character array for the name. - An instance
p1
is initialized with values. - The
memmove()
function copiesp1
top2
. - The copied values in
p2
are printed to confirm successful copying.
Output:
Copied Person ID: 101, Name: Arjun