memcpy in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The memcpy function in C is used to copy a specific memory size from a source location to the destination location. It copies the data directly from the source memory to the destination memory. The specified size of the memory to be copied should not exceed the size of the source memory. We must include the string.h header in our code to use the memcpy() function in C.

Syntax of memcpy() in C

According to the C99 standards, the syntax of the function memcpy in c is,

Before the introduction of C99 standards, the syntax of the memcpy function is,

Parameters of memcpy() in C

There are three parameters for the function memcpy in C,

  • The destination is a void pointer that is used to store the address of the destination memory location where the data going to copy.
  • The source is a constant void pointer that is used to store the address of the source memory location from where data is copied.
  • The size denotes the size of memory to be copied from the source to the destination.

The data types and keywords used in the syntax of memcpy in c are,

  • A void* or void pointer initially points to nothing and can be used to store the memory address of any data type.
  • The const keyword is used to indicate a variable that is unmodifiable or can't be reassigned once assigned a value.
  • The size_t is an unsigned integer data type, which means it can only be used to store positive values. This data type is used to store the size of an object or data type in bytes.

The only difference in syntax before and after the implementation of C99 standards is the restrict keyword. The restrict keyword makes the address location unique to a single pointer. This helps to overcome the problem of overlapping addresses.

Return Value of memcpy() in C

The memcpy function in C returns a void pointer that points to the starting address of the destination memory location in which final values are stored.

Example

Let us see a simple example for memcpy in c in which the data present in the memory location of an array is overwritten to the memory location of another array,

Output:

The data in the destination has been overwritten with the data present in the source. The above example illustrates copying the data from the location of array, b, and storing it in memory at the location of array a. It is important to note that we are copying values directly from memory to memory.

The sizeof() operator is used to find the size of data types and return the size in bytes. In our example, the integer array has two values, so we want a space of 2 int variables to be copied from source to destination.

An interesting question here is, where are we using a pointer? According to the syntax, we must pass the address of the array to be stored in the pointer as arguments, but we are passing the array variable representing the values stored in the array. This is possible because the compiler always handles arrays as a pointer, so the compiler converts the array to a pointer before the execution of a function.

The function memcpy(a, b, sizeof(int)*2); will be executed as the following steps,

The address of the first value present in the array a and b is stored in the pointer. The addresses of the consecutive values in the array can be accessed by incrementing the pointer as an array stores data in consecutive memory locations.

Exceptions of memcpy() in C

  • There are possibilities for a buffer overflow to occur on using memcpy in c. The buffer overflow occurs when the size of the memory that is provided as a parameter is larger than the destination memory. Consider the following example,

In the above program, the total size of the destination is only 5 bytes, but we are trying to write 35 bytes from the source to the destination. Hence, the above example code will cause a buffer overflow. This may lead to the remaining 30 bytes written to parts of memory that have confidential information or may contain malicious instructions.

To prevent buffer overflow, there is a modified version of memcpy introduced in C11 standards called the memcpy_s. The syntax of memcpy_s is,

In addition to three parameters in memcpy, the memcpy_s have one more parameter called the destination_size. This parameter depicts the maximum size of the destination memory.

This prevents buffer overflow by restricting the space on the destination memory to the value of destination_size. If the size value specified is larger than the destination_size, the program will stop execution and an error is returned. The error returned has a data type of errno_t.

We have used rsize_t here as the data type for memory. The difference between size_t and rsize_t is that rsize_t is used for storing the memory of a single object and also has a maximum size limit called RSIZE_MAX. When the size exceeds RSZIZE_MAX an error is thrown and the program stops execution.

What is memcpy() in C?

  • memcpy() in C is used to copy specified bytes of memory from source address to destination address.
  • The memcpy function returns a void pointer which stores the address of the destination.
  • The memcpy function uses pointers to store the address of source and destination.
  • In C memcpy function is defined in string.h header file.

More Examples

The most useful application of memcpy in C is copying a structure at the address of the source to another structure at the destination.

A structure is a user-defined data type that can be used to store data of different or same data types together. In the above example, we have created a structure with three variables representing a book.

The name of the book is defined separately and is copied into the book_name present inside the structure because we can't directly assign a string to the character array present inside the structure as this kind of assignment is only possible on initializing the character array. For example, the following is not possible

We have to explicitly use the & symbol to specify that we are passing the address of the corresponding structures. It is important to do this, as the compiler doesn't automatically convert a structure to a structure pointer as in the case of arrays.

The output is,

We can also append strings by calculating the address to which we should copy the strings using memcpy in c.

In the above example, after copying the string source1 to destination, the pointer has been incremented by the length of the first string to reach the location next to where source1 have been stored. Then we store the next string source2 in this location. We are performing two copy operations to achieve concatenation of strings.

Output:

Conclusion

  • The memcpy function in C is used to copy data from a memory location to another memory location.
  • The function uses pointers to represent the address in which the data are stored.
  • The memcpy function returns a pointer that stores the address of the destination after the copy has been performed.
  • There are possibilities for buffer overflow using memcpy in C.
  • We can also use the memcpy function to append two strings together.