YouTip LogoYouTip

C Function Memmove

Here's the translated HTML content with all code blocks preserved: Title: C Library Function – memmove() | Rookie Tutorial [![Image 1: C Standard Library - string.h](#) C Standard Library - string.h](#) * * * `memmove()` is a memory operation function in the C standard library ``, used to copy a specified number of bytes from a source address to a destination address, with safe handling of overlapping memory regions. **Word meaning**: `memmove` is short for `memory move`, indicating data movement in memory. The C library function `void *memmove(void *str1, const void *str2, size_t n)` copies **n** bytes from **str2** to **str1**. **Differences from memcpy()**: * When the destination and source regions overlap in memory, `memmove()` ensures that overlapping bytes are safely transferred to the destination before the source data is overwritten, making it more reliable than `memcpy()`. * If the destination and source regions do not overlap at all, `memmove()` behaves identically to `memcpy()`. * When unsure whether two memory blocks overlap, `memmove()` should be preferred. * * * ## Function Declaration void *memmove(void *str1, const void *str2, size_t n) ### Parameters * **str1**: Destination address, pointing to the memory area where the copied content will be stored (type cast to `void*`). * **str2**: Source address, pointing to the starting position of the data to be copied (type cast to `const void*`). * **n**: Number of bytes to copy (of type `size_t`). ### Return Value Returns a pointer to the destination storage area `str1` (same as the input `str1`). ### Header File #include * * * ## Examples ### Example 1: Basic Usage The following example demonstrates `memmove()` usage in both non-overlapping and overlapping scenarios: ## Example #include #include int main() { /* Scenario 1: Non-overlapping memory copy */ char dest[]="oldstring"; char src[]="newstring"; printf("Before non-overlapping copy: dest = %s, src = %sn", dest, src); memmove(dest, src, 9); printf("After non-overlapping copy: dest = %s, src = %sn", dest, src); /* Scenario 2: Overlapping memory copy (where memmove excels) */ char str[]="123456789"; printf("n Before overlapping copy: %sn", str); memmove(str +2, str, 6); printf("After overlapping copy: %sn", str); return 0; } **Output:** Before non-overlapping copy: dest = oldstring, src = newstring After non-overlapping copy: dest = newstring, src = newstring Before overlapping copy: 123456789After overlapping copy: 121234569 **Code Analysis:** 1. In Scenario 1, `dest` and `src` are independent arrays without overlap. `memmove(dest, src, 9)` copies the first 9 bytes from `src` to `dest` as-is. 2. In Scenario 2, `str + 2` (destination) and `str` (source) point to overlapping regions within the same array. `memmove` first saves the data to be overwritten before completing the copy, yielding correct results. 3. If Scenario 2 used `memcpy` instead, some compilers or platforms might produce incorrect results since `memcpy` doesn't guarantee handling overlapping cases. ### Example 2: Handling Overlapping Memory `memmove()` still produces correct results when memory regions overlap: ## Example #include #include int main() { char str[]="memmove can be very useful"; printf("Before: %sn", str); /* Copy 7 bytes starting from position 11 to position 5 (regions overlap) */ memmove(str +5, str +11, 7); printf("After: %sn", str); return 0; } **Output:** Before: memmove can be very useful After: memmove can be very useful **Code Analysis:** * In the original string, position 5 onwards is `"ve can be very useful"`, and the 7 bytes starting from position 11 are `"can be"` (including trailing space). * `memmove(str + 5, str + 11, 7)` moves `"can be"` to position 5. Although the regions overlap, `memmove` selects a forward or backward copy strategy based on address comparison to ensure data integrity. * After copying, the content starting at position 5 becomes `"can be"`, while other characters remain unchanged. ### Example 3: Comparing memcpy() and memmove() This example shows potential differences between the two functions in identical overlapping scenarios: ## Example #include #include int main() { char str1[]="abcdefghij"; char str2[]="abcdefghij"; printf("Original: %sn", str1); /* Using memcpy: Behavior undefined when overlapping */ memcpy(str1 +2, str1, 5); printf("memcpy: %sn", str1); /* Using memmove: Behavior guaranteed when overlapping */ memmove(str2 +2, str2, 5); printf("memmove: %sn", str2); return 0; } **Output:** Original: abcdefghij memcpy: ababababij memmove: ababcdfghij **Note**: In this example, `memcpy` performs byte-by-byte forward copying, causing premature overwriting of source data and incorrect output. Meanwhile, `memmove` detects overlap and uses backward copying for correct results. Different platforms or compilers may implement `memcpy` differently, so `memmove` should always be used in overlapping scenarios. * * * ### Example 4: Copying Specific Byte Count `memmove()` operates at byte level and doesn't automatically append a null terminator. Manual addition of `''` is required: ## Example #include #include int main() { char src[]="Hello, World!"; char dest; /* Copy only first 5 bytes */ memmove(dest, src, 5); dest='';/* Manually add string terminator */ printf("Source: %sn", src); printf("Dest: %sn", dest); printf("Length: %zun", strlen(dest)); return 0; } **Output:** Source: Hello, World!Dest: HelloLength: 5 **Code Analysis:** * `memmove(dest, src, 5)` copies only the first 5 bytes (`'H' 'e' 'l' 'l' 'o'`). * `memmove()` doesn't automatically write `''` at the destination end. Omitting `dest = ''` would cause subsequent string operations (like `printf`, `strlen`) to read indeterminate memory, leading to undefined behavior. * * * ## Differences from memcpy() | Feature | memcpy() | memmove() | | --- | --- | --- | | Overlapping memory handling | Behavior undefined, may produce errors | Safely handles overlap, ensures correct copy | | Implementation | Direct sequential byte-by-byte copy | Detects overlap direction, chooses forward/backward copy | | Performance | Usually slightly faster (no overlap check overhead) | Slightly slower (includes overlap detection logic) | | Usage recommendation | Use when source and destination definitely don't overlap | Use when overlap exists or is uncertain | * * C Standard Library - string.h](#)
← C Function MemsetC Function Memcpy β†’