ProgrammingBackend C Developer

How to implement safe memory copying between arrays in C? Which standard library functions to use, and what is their difference?

Pass interviews with Hintsage AI assistant

Answer

Background

Safe memory copying is one of the most common and critical tasks in C. Initially, programs used a loop for byte-by-byte copying. Later, standard functions were added: memcpy, memmove, as well as more secure alternatives like strncpy for strings. Incorrect copying can lead to buffer overflows and vulnerabilities.

Problem

Using an inappropriate function or incorrect size when copying can corrupt data or even open a path for security-intrigued third parties. Distinguishing cases when memmove is used versus when memcpy is acceptable is critically important to avoid errors with overlapping memory areas.

Solution

For safe copying of non-string memory, use:

memcpy(dest, src, n);
  • memcpy quickly copies n bytes from src to dest if their memory areas do not overlap.
  • If the areas may overlap, use memmove:
memmove(dest, src, n);

For copying strings, use either strncpy, or better, modern and secure alternatives if available (strlcpy).

Key Features:

  • memcpy is safe only when there is no overlap between src and dest.
  • memmove works correctly even with overlapping memory areas.
  • For strings, it is better to use specialized functions, considering the null terminator.

Tricky Questions.

1. What is the difference between memcpy and memmove, and when is each function necessary?

memcpy works very fast but is not intended for overlapping memory areas — the result will be unpredictable. memmove guarantees correct copying regardless of overlap:

int arr[] = {1,2,3,4,5}; // memmove: safely copying with overlap memmove(arr+1, arr, 4 * sizeof(int));

2. Is it safe to use strcpy to copy a string into a smaller buffer?

No, the strcpy function does not check the size of the destination buffer. This often leads to buffer overflow. It is better to use strncpy, or strlcpy (if available), or explicitly control the size:

char dest[5]; strncpy(dest, src, sizeof(dest)-1); // src should be shorter than 5 characters

3. Can memcpy be used to copy structures with pointers?

Yes, but it only copies the pointers themselves, not the data at those addresses. This can lead to memory errors and double free issues.

Common Mistakes and Anti-Patterns

  • Using memcpy with overlapping memory areas.
  • Ignoring the size of the destination buffer.
  • Copying structures that contain pointers using simple memcpy (shallow copy).

Real-Life Example

Negative Case

A long string is copied into a small buffer using strcpy without checking the length of the source. A buffer overflow occurs, leading to a vulnerability.

Pros:

  • Minimal code, fast execution.

Cons:

  • Security vulnerability, possible crashes, memory corruption.

Positive Case

strncpy or memmove is used with precise size control, ensuring that the null terminator of the string is manually provided.

Pros:

  • Predictable behavior, protection against buffer overflow.

Cons:

  • Requires additional length control and possibly a slight reduction in speed due to extra checks.