The memcpy() function in C is used to copy a specified number of bytes from one memory location to another. It is widely used for copying arrays, structures, and blocks of memory efficiently.
- Defined in the <string.h> header file.
- Copies data in a byte-by-byte manner.
- Works with raw memory addresses regardless of the data type.
- Commonly used for fast memory manipulation operations.
#include <stdio.h>
#include <string.h>
int main() {
// Initialize a variable
int a = 20;
int b = 10;
printf("Value of b before calling memcpy: %d\n", b);
// Use memcpy to copy the value of 'a' into 'b'
memcpy(&b, &a, sizeof(int));
printf("Value of b after calling memcpy: %d\n", b);
return 0;
}
Output
Value of b before calling memcpy: 10 Value of b after calling memcpy: 20
Explanation
- The variables a and b are initialized with different values.
- memcpy(&b, &a, sizeof(int)) copies the bytes of a into b.
- After the copy operation, b contains the same value as a.
- The original value of b is overwritten by the copied data.
Syntax
void *memcpy(void *to, const void *from, size_t numBytes);
Parameters
- to: A pointer to the memory location where the copied data will be stored.
- from: A pointer to the memory location from where the data is to be copied.
- numBytes: The number of bytes to be copied.
Return Value: Returns a pointer to the destination memory location (to).
Note: The source and destination memory regions must not overlap. If overlapping memory regions need to be copied, use memmove() instead.
Copying a String using memcpy()
The memcpy() function can also be used to copy the contents of one character array (string) to another.
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Geeks";
char str2[6] = "";
// Copies contents of str1 to str2
memcpy(str2, str1, sizeof(str1));
printf("str2 after memcpy:");
printf("%s",str2);
return 0;
}
Output
str2 after memcpy:Geeks
Explanation
- str1 contains the string "Geeks" along with the terminating null character ('\0').
- sizeof(str1) returns the total size of the array, including the null terminator.
- memcpy() copies all bytes from str1 to str2.
- After copying, str2 contains the same string as str1.
Behavior and Limitations of memcpy()
The memcpy() function is optimized for fast memory copying, but it has certain characteristics and limitations that should be understood to avoid unexpected behavior.
- Copies memory byte by byte without performing type checking or data conversion.
- Does not check buffer boundaries, so copying more bytes than the destination can hold may cause undefined behavior.
- Source and destination memory regions must not overlap.
- Does not initialize memory before copying.
- Performs a shallow copy by copying raw memory contents from one location to another.
- When copying pointers, memcpy() copies the pointer value (memory address), not the data pointed to by the pointer.
- The programmer must ensure that both memory regions are valid and large enough for the copy operation.