Introduction to C++ memcpy
Whenever there is a need to copy a block of memory from one location to another location in C++, we make use of a function called memcpy() function where one memory location acts as a source whose contents are to be copied to another memory location that acts as a destination and both the source memory location and destination memory location are pointed by the pointers and cstring.h header file must be included in the C++ program to be able to make use of memcpy() function and the number of bytes to be copied from source memory location to the destination memory location is specified as a parameter to the memcpy function along with source and destination memory locations.
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 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 location in C++, we make use of 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.
- Both the source memory location and destination memory location are pointed by the pointers.
- 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 source is the source of the memory location from where the contents is to be copied, 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 along with 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 cstring.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 is displayed as the output on the screen. The output is shown in the snapshot above.
4.5 (4,545 ratings)
View Course
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 cstring.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.
Conclusion
In this tutorial, we understand the concept of memcpy() function in C++ through 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 few examples respectively. You may also have a look at the following articles to learn more –