C++

How to Use the C++ Memcpy Function

In programming languages, while creating some logical modules for our programs, we need to copy our content from one array of any data type to another array. This duplication may be of the whole array or can be customized by providing a specified size up to which we need to copy the content of the array. This phenomenon can be done through different approaches. Among them is a function in the C++ programming language “memcpy”. This is used to copy the memory in the form of blocks.

Working of Memcpy

Memcpy copies data bytes by byte from the source array to the destination array. This copying of data is threadsafe. The process of copying data can fail if the given size is not accurate for the destination array. At some point, the behaviors of the memcpy() function become undefined depending on the condition. Sometimes, the objects overlap, and there is a null pointer at any of the two locations, either source or destination.

Syntax of Memcpy

# memcpy(void destination, const void source, size count);

Here, destination and source are the locations from where the contents are copied and to which place they are copied. The third parameter shows the number of bytes that are to be copied. The return value from this built_in function is the location of the destination array.

Implementation of the Memcpy() Function

Example 1

This example contains the copy of data from the source to the destination part. First, we need to use two libraries and a header file in our source code. One is the iostream library that enables cin and cout in C++ programs and effectively uses user involvement. But, in this program, we only need to display the resultant values; that’s why we need to mention it. The second library used is the library that allows the copying of data from the source to the destination. This copying and allocation of memory are done through a library named cstring. So to make the program in a working state, we will first include these two libraries in the header file.

#include <cstring>

#include <iostream>

All the string or character array functionalities are done using a library cstring. In the main program, two character arrays are initialized. One is a source array, and the other is the destination array. Both these arrays are filled with data that we need to copy. In this code, we will copy all the data of one array to another. All the bytes will be duplicated in the destination array as the data is copied byte by byte. Here, both arrays have data.

There could be a possibility, when we copy bytes from one array to another and when the destination array is empty. These types of examples will be discussed later in the article.

For now, we will use the memcpy function to take the source and destination arrays as a parameter and the sizeof source to copy easily.

Memcpy(destination, source, sizeof(source));

The third parameter will determine the size of the source character array. Next, use the cout statement to display the content in the destination array.

We always use a compiler to compile. Then, execute the code, so we use G++. The “-o” will save the output of the code present inside the input file.

$ g++ -o mem mem.c

$ ./mem

The source array contains “Linux hint” from the resultant value, and the destination has “article”. Now, the destination array contains the data of the source array.

Example 2

This example will work on the same phenomenon, but we will copy the data from the source to the destination up to a specific limit. For instance, the source character array contains the same data as “Linux hint”, and the destination array has “Memcpy”. So we need to copy the data from the destination to the source of 4 bytes. For this purpose, we will mention the number of bytes as the parameter. While copying the specific number of bytes, the data in the destination array will get replaced by that of the source file.

Memcpy (destination, source, 4);

Here, we don’t need to calculate the size of the source array. Only a number is to be mentioned where we want to copy data.

You will see that the 4 bytes from the source are copied to the destination. For instance, “Linux” from “Linux hint” is extracted and copied to the destination array. Whereas, in the destination array, the first 4 bytes are removed and are replaced through the bytes of the source array. The rest of the bytes in the destination array will remain the same.

Example 3

In this example, we will have integer values in the source array. This time, a source array of specific size is defined to store data up to some extent. As we have mentioned earlier, the data from the source will be copied now into the empty array. Next, we will declare a destination array of a specific size. In the following example, we have mentioned the size of the array as 7, which means we will copy 7 integers in the destination array:

Int destination[7];

Now, we will use the memcpy function to copy the integer values of 7 numbers to avoid duplicating whole data from the source file. Here, we will use destination, source array, and the total size of the integer array multiplied by 7 to calculate the actual size:

# Memcpy (destination, source, sizeof(int) * 7);

Next, we will display data using a “FOR” loop, unlike in the previous examples. As each number is separated through the index of an array, so each index contains a separate value. A for loop will continue iterating up to the 7th position. Now, save the code for the compilation.

As a result, 7 integers are copied. This contains a total of 10 bytes:

Example 5

In the last example, we have copied the integer values into the empty array. Here, we will copy the characters from the character source array to the empty destination character array.

First, initialize the source array with the string. While introducing the destination character array, only declare the size of the array and keep it empty. Apply the same memcpy function to copy the content of the source array to the destination file. We have applied a limit to copy the data, as we did before. So we want to transfer data of 9 bytes, as we have taken the size of the destination array as 10.

Memcpy (destination, source, sizeof(char)*9);

As the size is applied to the array, we will use a loop to display the content of the character destination array. This will display the values present in each index of the destination array. The data of the source up to the 9th byte, including space, is shown below:

Conclusion

The article “C++ memcpy” is a source of knowledge regarding the built-in function in the C++ programming language that deals with copying data from one array to another array. There are many possibilities in copying data from one array two another. These are explained with the help of examples to remove any ambiguity regarding the use of the memcpy() function. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.