The memcpy() function from <string.h> copies exactly n bytes from a source block to a destination block. It is one of the fastest ways to duplicate strings, arrays, and raw binary data—provided the destination is large enough and the regions do not overlap.
01
Copy n Bytes
Raw transfer.
02
Returns dest
Same pointer.
03
Buffer Size
Avoid overflow.
04
No Overlap
Use memmove.
05
+1 for Strings
Include \0.
06
vs strcpy()
Explicit length.
Fundamentals
Definition and Usage
In C, memcpy() is declared in <string.h>. It performs a byte-for-byte copy from src to dest for n bytes. It does not interpret the data as text—it simply moves bytes.
When copying C-strings, you must copy the null terminator if you want dest to be a valid string. That means copying strlen(src) + 1 bytes, not just strlen(src). The destination array must have at least that much space reserved.
💡
Beginner Tip
If source and destination memory regions overlap, memcpy() has undefined behavior. Use memmove() instead when copying within the same buffer or when ranges might overlap.
Nine visible characters plus one null byte equals 10 bytes total. destination[16] has enough room. Without the + 1, the copy would omit '\0' and printing destination would be unsafe.
Example 2 — Copy Only the First n Bytes
Copy a prefix without making a full string—useful for fixed-width fields.
The first copy writes "Hello, " at the start of buffer. The returned pointer (buffer) is advanced implicitly by copying the next piece at offset 7. Ensure the total size still fits in buffer.
Example 5 — Overlap: memcpy() vs memmove()
When regions overlap, use memmove()—not memcpy().
C
#include <stdio.h>
#include <string.h>
int main(void) {
char text[] = "abcdef";
/* Safe: shift "bcdef" left by one position */
memmove(text, text + 1, 5);
text[5] = '\0';
printf("After memmove: %s\n", text);
/* memcpy(text, text + 1, 5); // UNDEFINED if overlapping */
return 0;
}
📤 Output:
After memmove: bcdef
How It Works
Source and destination both lie inside text, so they overlap. memmove copies in the correct direction to avoid clobbering unread data. Using memcpy here would be undefined behavior even if it sometimes appears to work.
Applications
🚀 Common Use Cases
String duplication — copy text into a new buffer when you control the destination size.
Array copying — duplicate struct arrays or byte buffers in one call.
Protocol packets — assemble fixed-layout messages from template bytes.
Graphics / audio — copy pixel or sample blocks between buffers.
Serialization — move raw memory into an output buffer before writing to disk or network.
🧠 How memcpy() Works
1
You pass dest, src, and n
The function receives destination and source addresses plus the byte count to transfer.
Input
2
Copy n bytes
Each byte from src is written to the corresponding offset in dest.
Transfer
3
Return dest
The original destination pointer is returned for convenience.
Result
=
📋
Destination updated
The first n bytes of dest now match src. Verify size and overlap rules were respected.
Important
📝 Notes
Buffer size — dest must have room for at least n bytes. Writing beyond the buffer is undefined behavior.
No overlap — if src and dest overlap, use memmove() instead.
Null terminator — copying strlen(s) bytes alone does not make a C-string; include + 1 or append '\0' manually.
Not for allocation — memcpy does not allocate memory; you must provide both buffers.
Order of evaluation — do not rely on overlapping read/write side effects with memcpy.
Performance
⚡ Optimization
Standard library memcpy() implementations are heavily optimized—often using word-aligned bulk copies. For large transfers, calling memcpy directly beats a naive byte loop. Copy only the bytes you need; avoid redundant copies in hot paths.
Wrap Up
Conclusion
memcpy() is the standard way to copy raw memory in C. Size your destination correctly, include the null byte when duplicating strings, and switch to memmove() when regions might overlap.
Practice the examples, then continue to memmove() for safe copying when source and destination share the same buffer.
Zero-terminate manually after partial string copies
❌ Don’t
Copy into a buffer smaller than n
Use memcpy on overlapping regions
Forget the null terminator when you need a C-string
Assume memcpy allocates memory
Use memcpy where strcpy is clearer and safe
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about memcpy()
Use these points whenever you copy memory in C.
5
Core concepts
📋01
Copy n Bytes
Raw transfer.
Basics
📝02
Returns dest
Same pointer.
Return
🔒03
Size dest
Prevent overflow.
Safety
📈04
+1 for \0
String copies.
Strings
💬05
Use memmove
If overlap.
Rule
❓ Frequently Asked Questions
memcpy() copies n bytes from a source memory block to a destination block. It returns the dest pointer. The copy is a raw byte transfer—it does not add a null terminator unless you include that byte in n.
void* memcpy(void* dest, const void* src, size_t n); Include <string.h>. dest is where bytes are written, src is the source, and n is how many bytes to copy.
It returns dest—the same pointer you passed as the destination. This allows chaining such as memcpy(a, b, n) in expressions, though assigning the result is optional.
memcpy() requires that src and dest do not overlap; overlapping copies have undefined behavior. memmove() handles overlapping regions safely by copying in the correct direction. Use memmove when buffers might overlap.
Copy strlen(src) + 1 bytes to include the terminating '\0': memcpy(dest, src, strlen(src) + 1). Ensure dest has at least that many bytes allocated. For plain string duplication, strcpy() or strncpy() may be clearer.
Yes. If n is larger than the destination buffer, memcpy writes past the end—undefined behavior and a common security bug. Always ensure dest has room for at least n bytes before calling memcpy.
Did you know?
Many compilers treat memcpy of small, constant sizes as inline instructions. Even so, the C standard still requires you to respect buffer bounds and the no-overlap rule—optimizations do not make invalid calls safe.