Updated July 1, 2023
Introduction to C++ memcpy
In C++, when there is a need to copy a block of memory from one location to another, the memcpy() function is commonly used. This function copies the contents of a source memory location to a destination memory location. Both the source and destination memory locations are indicated by pointers. To utilize the memcpy() function, the cstring header file must be included in the C++ program.
Syntax:
memcpy(void *destination, const void *source, size_t number_of_bytes)
where *destination represents the pointer to the destination memory location, *source represents the pointer to the source memory location, and number_of_bytes represents the number of bytes to be copied from the source memory location to the destination memory location.
Working of memcpy() Function in C++
- Whenever there is a need to copy a block of memory from one location to another in C++, we use a function called memcpy() function.
- The memory location whose contents are to be copied to another memory location acts as a source, and the memory location to which the contents are going to be copied acts as a destination.
- The pointers point to both the source memory location and destination memory location.
- The cstring.h header file must be included in the C++ program to be able to make use of memcpy() function to copy the contents of the source memory location to the destination memory location.
- The memcpy() function takes three parameters, namely source, destination, and a number of bytes where the source is the source of the memory location from where the contents are to be copied, and the destination is the memory location to which the contents are to be copied.
- The number of bytes to be copied from the source memory location to the destination memory location is specified as a parameter to the memcpy function and source and destination memory locations.
- The contents of the source memory location overlap the contents of the destination memory location after copying is done using memcpy() function.
Examples
C++ program to demonstrate the use of memcpy() function to copy the contents of the source memory location to the destination memory location by the amount specified by the number of bytes as a parameter to the memcpy() function:
Example #1
//the headers cstring and iostream are included to be able to make use of cin, cout, and memcpy() functions.
Code:
#include <cstring>
#include <iostream>
using namespace std;
//main method is called
int main()
{
//two arrays called source and destination are defined among which an array od characters is stored in a variable called source and these contents are going to be copied to the destination variable
char source[15] = "Welcome to C++";
char destination[8];
//memcpy function is called to copy the contents of source to destination by the amount specified by the number of bytes
memcpy(destination,source,sizeof(char)*7);
cout << "The contents of the destination after copying the contents of source is:" << "\n" << endl;
for (int c=0; c<7; c++)
//the copied contents of the destination is displayed as the output on the screen
cout << destination[c];
return 0;
}
Output:
In the above program, we are able to use cin, cout, and memcpy functions by including the headers iostream. h and string. h. Then the main method is called, within which two arrays of characters are defined, called source and destination. An array of characters is stored in the variable called source. Then the memcpy() function is called to copy the contents of the source memory location to the destination memory location by the amount specified by the number of bytes. Then the copied contents in the destination are displayed as the output on the screen. The output is shown in the snapshot above.
Example #2
C++ program to demonstrate the use of memcpy() function to copy the contents of the source memory location to the destination memory location by the amount specified by the number of bytes as a parameter to the memcpy() function:
//the headers cstring and iostream are included to be able to make use of cin, court, and memcpy() functions
Code:
#include <cstring>
#include <iostream>
using namespace std;
//main method is called
int main()
{
//two arrays called source and destination are defined among which an array od characters is stored in a variable called source and these contents are going to be copied to the destination variable
char source[16] = "Learnin is fun";
char destination[8];
//memcpy function is called to copy the contents of source to destination by the amount specified by the number of bytes
memcpy(destination,source,sizeof(char)*7);
cout << "The contents of the destination after copying the contents of source is:" << "\n" << endl;
for (int c=0; c<8; c++)
//the copied contents of the destination is displayed as the output on the screen
cout << destination[c];
return 0;
}
Output:
In the above program, we are able to use cin, cout, and memcpy functions by including the headers iostream. h and string. h. In the main method, two character arrays named “source” and “destination” are defined. The source array contains a sequence of characters. The memcpy() function is invoked to copy the contents of the source memory location to the destination memory location based on the specified number of bytes. Subsequently, the copied contents in the destination array are displayed as the output on the screen.
Conclusion
In this tutorial, we understand the concept of memcpy() function in C++ through the definition, syntax, and working of memcpy() function through programming examples and their outputs.
Recommended Articles
This is a guide to C++ memcpy. Here we discuss the introduction and Working of memcpy() Function in C++, along with a few examples respectively. You may also have a look at the following articles to learn more –