memcpy() in C++ Last Updated : 15 May, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report C++ memcpy() function is a standard library function that is used to copy the specified number of bytes from one memory location to another memory location regardless of the type of data stored. It is optimized for copying memory chunks in an efficient manner and it copies memory in a byte-by-byte format.Example: C++ #include <iostream> #include <cstring> using namespace std; int main() { // Initialize a variable char a = 'A'; char b = 'B'; // Use memcpy to copy the value of // 'a' into 'b' memcpy(&b, &a, sizeof(b)); cout << a << " " << b; return 0; } OutputA ASyntaxThe memcpy() function in C++ is defined in the <cstring> and <string.h> header files. C++ memcpy(to, from, num); Parametersto: Pointer to the memory location where the copied data will be stored.from: Pointer to the memory location from where the data is to be copied.num: The number of bytes to be copied.Return ValueThis function returns a pointer to the memory location where data is copied.Examples of memcpy() in C++The below examples show how to use memcpy() function in our C++ programs:Copying Array using memcpy() C++ #include <iostream> #include <cstring> using namespace std; int main() { int s[5] = {10, 20, 30, 40, 50}; int d[5]; // Copy the contents of s to d memcpy(d, s, sizeof(s)); // Destination array after copying for (int i = 0; i < 5; ++i) cout << d[i] << " "; cout << endl; return 0; } Output10 20 30 40 50 Copying C-Style Strings C++ #include <iostream> #include <cstring> using namespace std; int main() { char s[] = "GeeksforGeeks"; int d[20]; // Copy the contents of s to d memcpy(d, s, sizeof(s)); // Destination string after copying cout << s; return 0; } OutputGeeksforGeeksImportant Points about memcpy()The memcpy() function is an old function which C++ inherited from C, so it is to be used with care. Keep in mind the following points:memcpy() doesn't check for overflow or \0.memcpy() function copies the memory in a byte-by-byte format without any checks or transformations, meaning it does not handle type conversions or alignment issues.If the destination already contains some data then memcpy() function simply overwrites the data. The memcpy() function in C++ creates a shallow copy as it only copies the raw bytes of the memory from one location to another. It does not perform a deep copy or handle higher level objects. memcpy() cannot handles overlapping memory regions and may lead to undefined behaviour when source and destination addresses overlap.The memcpy() function simply copies bytes without initialising any memory. memcpy() only copies the pointer values (i.e., the addresses they hold), not the actual objects or data those pointers reference.Note: memmove() is another library function that handles overlapping well. Comment More infoAdvertise with us Next Article memcpy() in C++ T tauheeda834k Follow Improve Article Tags : C++ cpp-string CPP-Functions Practice Tags : CPP Similar Reads memcpy() in C The memcpy() function in C is defined in the <string.h> header is a part of the standard library in C. The memcpy() function is used to copy a block of memory from one location to another. Example: C#include <stdio.h> #include <string.h> // For memcpy int main() { // Initialize a v 2 min read std::memcmp() in C++ memcmp() function compares the first count bytes ( given number of characters ) of the memory pointed to by buf1 and buf2. memcmp() is a Standard Library function defined in <string.h> header file in C++. Syntaxint memcmp(const void *buf1, const void *buf2, size_t count);Parametersbuf1: Pointe 3 min read strcpy in C++ strcpy() is a standard library function in C++ and is used to copy one string to another. In C++ it is present in the <string.h> and <cstring> header files. Syntax: char* strcpy(char* dest, const char* src);Parameters: This method accepts the following parameters: dest: Pointer to the de 2 min read Memset in C++ C++ memset() is a function that copies a single character for a specified number of times to the given bytes of memory. It is useful for filling a number of bytes with a given value starting from a specific memory location.Example:C++#include <bits/stdc++.h> using namespace std; int main() { c 5 min read std::memchr in C++ C++ offers various standard template library functions to be used. One of them is memchr() function which is used to search for the first occurrence of a character in a specified number of characters. memchr() is defined inside <cstring> header file. Syntax of memchr()const void* memchr( const 2 min read Like