C Function Memcpy
## C Library Function - memcpy()
The `memcpy()` function is a core utility in the C Standard Library (``). It is used to copy a specified number of bytes from a source memory location to a destination memory location. Unlike string-specific copy functions like `strcpy()`, `memcpy()` operates on raw memory blocks and does not stop copying when it encounters a null character (`\0`).
---
## Syntax
```c
void *memcpy(void *str1, const void *str2, size_t n);
```
### Parameters
* **`str1`** β A pointer to the destination array where the content is to be copied, type-cast to a `void*` pointer.
* **`str2`** β A pointer to the source of data to be copied, type-cast to a `const void*` pointer.
* **`n`** β The number of bytes to be copied.
### Return Value
This function returns a pointer to the destination memory area (`str1`).
---
## Code Examples
### Example 1: Copying a String to a Destination Buffer
This example demonstrates how to copy an entire string (including its null terminator) into a destination character array.
```c
#include
#include
int main()
{
const char src = "https://www.youtip.co";
char dest;
// Copy src to dest, including the null terminator (+1)
memcpy(dest, src, strlen(src) + 1);
printf("dest = %s\n", dest);
return 0;
}
```
**Output:**
```text
dest = https://www.youtip.co
```
---
### Example 2: Copying a Subsegment of a String
This example demonstrates how to copy a specific segment of memory by using pointer arithmetic. We copy 6 characters starting from the 12th character of the source string.
```c
#include
#include
int main()
{
char *s = "https://www.youtip.co";
char d;
// Copy 6 characters starting from index 12 ('y')
memcpy(d, s + 12, 6);
// Alternative syntax: memcpy(d, s + 12 * sizeof(char), 6 * sizeof(char));
// Manually append the null terminator to make it a valid C string
d = '\0';
printf("%s\n", d);
return 0;
}
```
**Output:**
```text
youtip
```
---
### Example 3: Overwriting Part of an Existing Buffer
This example shows how `memcpy()` can be used to overwrite a specific portion of an existing destination buffer.
```c
#include
#include
int main(void)
{
char src[] = "***";
char dest[] = "abcdefg";
printf("Before memcpy: %s\n", dest);
// Overwrite the first 3 characters of dest with src
memcpy(dest, src, strlen(src));
printf("After memcpy: %s\n", dest);
return 0;
}
```
**Output:**
```text
Before memcpy: abcdefg
After memcpy: ***defg
```
---
## Important Considerations
### 1. Memory Overlap (Undefined Behavior)
The `memcpy()` function does not handle overlapping memory regions. If the source (`str2`) and destination (`str1`) buffers overlap, the behavior is **undefined**.
* **Solution:** If there is a possibility that the source and destination memory regions overlap, you must use **`memmove()`** instead of `memcpy()`. `memmove()` guarantees safe copying even when buffers overlap.
### 2. Buffer Overflow
`memcpy()` does not perform any bounds checking. If the value of `n` is larger than the allocated size of the destination buffer (`str1`), it will cause a buffer overflow, leading to data corruption, crashes, or security vulnerabilities. Always ensure that:
$$\text{Size of } str1 \ge n$$
### 3. Type Safety
Because `memcpy()` accepts `void*` pointers, it bypasses C's type-checking system. You can copy any data type (structs, arrays, integers, etc.). Ensure that you calculate the byte size correctly using the `sizeof` operator when copying non-character types:
```c
int source_arr = {1, 2, 3, 4, 5};
int dest_arr;
memcpy(dest_arr, source_arr, 5 * sizeof(int));
```
YouTip