HomeLinuxThe way to Use the C++ Memcpy Operate

The way to Use the C++ Memcpy Operate


By offering the variety of bytes to repeat, the memcpy() operate in C++ is used to switch a sure variety of bytes from one reminiscence handle to a different. The vacation spot pointer, the supply pointer, and the variety of bytes that you just wish to copy are the three inputs that it is advisable make the most of the memcpy() methodology. It’s often utilized when enormous blocks of reminiscence have to be duplicated quick. We frequently use it in techniques programming and low-level operations the place we have to manipulate the info instantly.

Declaration of Memcpy() Operate in C++

Now, let’s perceive the declaration of the memcpy() operate in C++ programming language. First, the operate returns a void pointer to the vacation spot buffer. Then, we name the memcpy() operate. Within the operate brackets, we use the vacation spot as a pointer to the reminiscence location the place we wish to copy the info. The supply is a pointer to the reminiscence location the place the info is at the moment saved. And the num is the variety of bytes that we wish to copy.

The void* knowledge sort that’s used for each supply and vacation spot permits for flexibility in knowledge varieties. The size_t knowledge sort for num ensures that the variety of bytes which might be copied is appropriately represented.

Utilization of Memcpy() Operate in C++

This program demonstrates methods to use the memcpy() operate in C++ to repeat a string from one character array to a different. The principle() operate is first declared, adopted by the inclusion of the important header information, iostream, and cstring.

#embody <iostream>
#embody <cstring>
utilizing namespace std;
int primary() {
    char supply[] = “Howdy, World!”;
    char vacation spot[20];
    memcpy(vacation spot, supply, sizeof(supply));
    vacation spot[sizeof(source)] = ;
    cout << “The Supply String is: “ << supply << endl;
    cout << “The Vacation spot String is: “ << vacation spot << endl;
    return 0;
}

In the principle() operate, two character arrays are declared: supply and vacation spot. The “Howdy, World!” textual content is used because the supply’s initialization worth, whereas the vacation spot is now empty. The contents of the supply are then copied into the vacation spot utilizing the memcpy() methodology. The arguments which might be handed to memcpy() are the vacation spot buffer vacation spot, the supply buffer supply, and the dimensions of the supply buffer sizeof(supply).

After the memcpy() operation is full, we add a null terminator on the finish of the vacation spot in order that the string is correctly terminated. Lastly, we print out the supply and vacation spot strings utilizing the cout statements to confirm that the copy is profitable. When this system is executed, it outputs the next:

Copying the Contents of a Struct to One other Struct Utilizing the Memcpy() Operate

This C++ code demonstrates the usage of the memcpy() operate to repeat the contents of a struct which is “Worker” to a different struct which is the “upt_employee_data”. We embody the fundamental header information in this system. Then, the “Worker” struct is outlined with three members: an int id, a personality array title of dimension 50, and a float wage.

#embody <iostream>
#embody <cstring>
utilizing namespace std;
struct Worker {
    int id;
    char title[50];
    float wage;
};
int primary() {
    Worker old_employee_data= {1, “John Doe”, 5000.0};
    Worker upt_employee_data;  
    memcpy(&upt_employee_data, &old_employee_data, sizeof(Worker));
    upt_employee_data.wage = 6000.0;
    cout << “Previous Knowledge of Worker: “ << old_employee_data.id << ” “ << old_employee_data.title << ” “ << old_employee_data.wage << endl;
    cout << “Up to date Knowledge of Worker: “ << upt_employee_data.id << ” “ << upt_employee_data.title << ” “ << upt_employee_data.wage << endl;
    return 0;
}

In the principle() operate, an “Worker” variable which is “old_employee_data” is initialized with the id worth of 1, the title worth of “John Doe”, and the wage worth of 5000.0. One other “Worker” variable which is the “upt_employee_data” is asserted however not initialized. The memcpy() operate is then used to repeat the contents of “old_employee_data” to “upt_employee_data”.

The primary argument to memcpy() is the vacation spot handle (&upt_employee_data). The second argument is the supply handle (&old_employee_data). And the third argument is the dimensions of the struct (sizeof(Worker)). After the copy, the salaried member of “upt_employee_data” is modified to 6000.0. Lastly, the unique values of “old_employee_data” and the up to date values of “upt_employee_data” are printed out to the console utilizing cout as you may see within the following:

Copying the Content material of One Array to One other Array

That is an instance that demonstrates methods to use the memcpy() operate to repeat the contents of 1 array to a different. The start of a C++ program that features the usual enter/output is the iostream library and the C-style string library cstring. Then, we use the road utilizing the std namespace which tells the compiler that we use the usual namespace std which incorporates numerous customary capabilities and objects akin to cout to print the output to the console.

#embody <iostream>
#embody <cstring>
utilizing namespace std;
int primary() {
    int source_array[] = {1, 2, 3, 4, 5, 6, 7};
    int destination_array[]  = {8, 9, 10, 11, 12, 13, 14};
    cout << “The Supply Array is: “;
    for (int i = 0; i < 7; ++i) {
        std::cout << source_array[i] << ” “;
    }
    cout << endl;
    cout << “The Previous Vacation spot Array is: “;
    for (int i = 0; i < 7; ++i) {
        cout << destination_array[i] << ” “;
    }
    cout << endl << endl;
    memcpy(destination_array, source_array, sizeof(source_array));
    cout << “The Up to date Vacation spot Array is: “;
    for (int i = 0; i < 7; ++i) {
        cout << destination_array[i] << ” “;
    }
    cout << endl;
    return 0;
}

For the reason that primary() operate is the place this system begins, we then name it. We’ve two integer arrays in the principle() physique: the source_array and the destination_array. The source_array incorporates the values {1, 2, 3, 4, 5, 6, 7} and the destination_array incorporates the values {8, 9, 10, 11, 12, 13, 14}.

First, we print out the values of each arrays utilizing the for-loop and cout. Then, we use the memcpy() operate to repeat the contents of the source_array to the destination_array. The primary argument to memcpy() is the vacation spot handle (destination_array), the second argument is the supply handle (source_array), and the third argument is the dimensions of the source_array (sizeof(source_array)).

After copying, we print out the contents of the destination_array once more to indicate that it has been up to date with the values from the source_array. This system then returns 0 which signifies that it runs efficiently.

Demonstrating the Use of Memcpy() with a Struct

This code creates a construction referred to as “Pupil” that has three components: id, a 20-element character array and title, and a float worth. The principle operate generates s1 and s2 scholar objects. The Id=1 and the names “Alice” and “3.5” are initialized on the primary merchandise which is s1. S2’s second merchandise is clean. The content material of s1 is subsequently copied into s2 utilizing the memcpy() methodology.

The handle of s2 and s1, in addition to the dimensions of the coed, are despatched to memcpy() on this method. The members of s2 are then modified, with the id=2, the title=”Bob”, and gpa=3.7 being set. With the assistance of the cout statements, the code then prints the info for each college students.

#embody <iostream>
#embody <cstring>
utilizing namespace std;
struct Pupil {
    int id;
    char title[20];
    float gpa;
};
int primary() {
    Pupil s1 = {1, “Alice”, 3.5};
    Pupil s2;
    memcpy(&s2, &s1, sizeof(Pupil));
    s2.id = 2;
    strcpy(s2.title, “Bob”);
    s2.gpa = 3.7;
    cout << “Pupil 1: “ << s1.id << ” “ << s1.title << ” “ << s1.gpa << endl;
    cout << “Pupil 2: “ << s2.id << ” “ << s2.title << ” “ << s2.gpa << endl;
    return 0;
}

The next is the output of the previoulsy-illustrated instance:

Conclusion

We discovered what the memcpy() operate within the C++ programming language is and why we use the memcpy() operate. We applied some examples based mostly on the memcpy() operate in order that we are able to perceive the ideas.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments