GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — C++ cstring memcpy() Function
Selected Reading
C++ cstring memcpy() FunctionThe memcpy() function in C++, is used for copying data from one memory buffer to another memory buffer. This function is defined in cstring header file. The function takes three arguments, it does not act like other higher-level functions. Also, it does not check overlapping memory regions and it performs a raw memory copy. This function does not throw any exception but can cause undefined behavior if the source and destination memory regions are null, overlapping or if size specified is greater than the actual size of the source memory region. SyntaxFollowing is the syntax of the C++ cstring memcpy() Function − void *memcpy(void *destination, const void *source, size_t num); ParametersFollowing are the parameters of this function −
Return ValueThe memcpy() function returns a pointer to the destination, which is the same as the destination pointer passed as the first argument. Example 1Let us understand how to use the memcpy() function with the help of an example. In this example, let's try to copy the content of one array to another array using the memcpy() function. #include <iostream>
#include <cstring>
using namespace std;
int main() {
char source[] = "Hello, World!";
char destination[100];
// Copying the content of source array to destination array
memcpy(destination, source, strlen(source) + 1);
cout << "Source: " << source << endl;
cout << "Destination: " << destination << endl;
return 0;
}
OutputWhen you run the program, the output will be − Source: Hello, World! Destination: Hello, World! Example 2Let us understand another example of using the memcpy() function. In this example, we will copy the content of one structure to another structure using the memcpy() function. #include <iostream>
#include <cstring>
using namespace std;
struct Employee {
char name[50];
int age;
float salary;
};
int main() {
struct Employee source = {"John Doe", 30, 50000.0};
struct Employee destination;
// Copying the content of source structure to destination structure
memcpy(&destination, &source, sizeof(source));
cout << "Source:" << endl;
cout << "Name: " << source.name << endl;
cout << "Age: " << source.age << endl;
cout << "Salary: " << source.salary << endl;
cout << endl;
cout << "Destination:" << endl;
cout << "Name: " << destination.name << endl;
cout << "Age: " << destination.age << endl;
cout << "Salary: " << destination.salary << endl;
return 0;
}
OutputWhen you run the program, the output will be: Source: Name: John Doe Age: 30 Salary: 50000.00 Destination: Name: John Doe Age: 30 Salary: 50000.00 Example 3In this example, we will try to copy the content of one array to another array using the memcpy() function. Here, we are creating two arrays, one source array and one destination array. We will then try to copy the content of the source array to the destination array using the memcpy() function, but we will pass the size greater than the actual size of the source memory region. #include <iostream>
#include <cstring>
using namespace std;
int main() {
char source[] = "Hello, World!";
char destination[100];
// Copying the content of source array to destination array
memcpy(destination, source, strlen(source) + 4);
cout << "Source: " << source << endl;
cout << "Destination: " << destination << endl;
cout << "Extra bites on destination: " << destination[strlen(source)+2] << endl;
return 0;
}
OutputWhen you run the program, the output will be: Source: Hello, World! Destination: Hello, World! Extra bites on destination: As you can see, the output is not as expected. This is because we passed the size greater than the actual size of the source memory region. This is an exception case of the memcpy() function. Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |