Implement memcpy
Implement void *my_memcpy(void *dst, const void *src, size_t n) that copies exactly n bytes from src to dst and returns dst. The regions do not overlap. Do not use any library function.
This is one of the most common embedded C interview questions. Be ready to explain why the pointers are cast to unsigned char * and how it differs from memmove.
Write your code on the right and press Run (or Ctrl + Enter). Hidden tests check your functions and print the result below the editor.
void * cannot be dereferenced or incremented in standard C. Convert both pointers to byte pointers (unsigned char *), copy one byte per iteration, and remember to return the original dst.
#include <stdio.h>
#include <stddef.h>
#include <string.h>
void *my_memcpy(void *dst, const void *src, size_t n)
{
unsigned char *d = dst; /* void * converts implicitly in C */
const unsigned char *s = src;
while (n--)
*d++ = *s++;
return dst; /* original pointer, not d */
}
Your version is correct. The casts (unsigned char *)dst are optional in C, because void * converts to any object pointer implicitly (they are required in C++). while (n--) is the same loop written shorter.
Why unsigned char? The standard guarantees that any object can be accessed byte by byte through a character type, which is exempt from strict aliasing. unsigned char has no sign or padding problems, so every bit pattern copies exactly.
Walk-through: copy 3 bytes "abc"
| n before | copy | d moves to |
|---|---|---|
| 3 | d[0] = 'a' |
d + 1 |
| 2 | d[1] = 'b' |
d + 2 |
| 1 | d[2] = 'c' |
d + 3 |
| 0 | loop ends, return original dst |
memcpy vs memmove: memcpy assumes the buffers do not overlap; its parameters are declared restrict, and overlapping calls are undefined behaviour. memmove handles overlap by copying backwards when needed (see Implement memmove).
Follow-ups interviewers ask:
- How would you make it faster? Copy bytes until dst is word-aligned, then copy 4 or 8 bytes at a time, then the remaining tail. Only do word copies if src is aligned too, or the CPU supports unaligned access (Cortex-M0 faults on it). Real libraries also use SIMD or DMA.
- Can the compiler replace your loop? Yes, GCC may turn the loop into a call to memcpy itself, which recurses forever if you are writing memcpy. Build library code with -fno-builtin or -ffreestanding.
- What about volatile memory such as device registers or DMA buffers? memcpy may merge or reorder accesses, so copy register blocks with an explicit loop over volatile pointers.
Show the hidden tests
/* ---- tests ---- */
static int fails;
#define CHECK(c) do { if (!(c)) { printf("FAIL line %d: %s\n", __LINE__, #c); fails++; } } while (0)
int main(void)
{
char src[] = "embedded";
char dst[16] = { 0 };
CHECK(my_memcpy(dst, src, 9) == dst && strcmp(dst, "embedded") == 0);
unsigned char raw[5] = { 0x00, 0xFF, 0x80, 0x7F, 0x00 }; /* zeros inside: not a string */
unsigned char out[5] = { 1, 1, 1, 1, 1 };
my_memcpy(out, raw, 5);
CHECK(memcmp(out, raw, 5) == 0);
int a[4] = { 1, -2, 300000, -400000 }, b[4] = { 0 };
my_memcpy(b, a, sizeof a);
CHECK(memcmp(a, b, sizeof a) == 0);
char g[4] = { 'x', 'x', 'x', 'x' };
CHECK(my_memcpy(g, "ab", 0) == g && g[0] == 'x'); /* n = 0 copies nothing */
my_memcpy(g + 1, "ab", 2);
CHECK(g[0] == 'x' && g[1] == 'a' && g[2] == 'b' && g[3] == 'x'); /* no overrun */
puts(fails ? "Some tests failed" : "All tests passed");
return fails != 0;
}