Open In App

memcpy() in C++

Last Updated : 15 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

C++ memcpy() function is a standard library function that is used to copy the specified number of bytes from one memory location to another memory location regardless of the type of data stored. It is optimized for copying memory chunks in an efficient manner and it copies memory in a byte-by-byte format.

Example:

C++
#include <iostream>
#include <cstring>
using namespace std; 

int main() {
    
    // Initialize a variable
    char a = 'A';
    char b = 'B';

    // Use memcpy to copy the value of
    // 'a' into 'b'
    memcpy(&b, &a, sizeof(b));

    cout << a << " " << b;
    return 0;
}

Output
A A

Syntax

The memcpy() function in C++ is defined in the <cstring> and <string.h> header files.

C++
memcpy(to, from, num);

Parameters

  • to: Pointer to the memory location where the copied data will be stored.
  • from: Pointer to the memory location from where the data is to be copied.
  • num: The number of bytes to be copied.

Return Value

  • This function returns a pointer to the memory location where data is copied.

Examples of memcpy() in C++

The below examples show how to use memcpy() function in our C++ programs:

Copying Array using memcpy()

C++
#include <iostream>
#include <cstring> 
using namespace std; 

int main() {
    int s[5] = {10, 20, 30, 40, 50};
    int d[5];

    // Copy the contents of s to d
    memcpy(d, s, sizeof(s));

    // Destination array after copying
    for (int i = 0; i < 5; ++i)
        cout << d[i] << " ";
    cout << endl;

    return 0;
}

Output
10 20 30 40 50 

Copying C-Style Strings

C++
#include <iostream>
#include <cstring> 
using namespace std; 

int main() {
    char s[] = "GeeksforGeeks";
    int d[20];

    // Copy the contents of s to d
    memcpy(d, s, sizeof(s));

    // Destination string after copying
    cout << s;

    return 0;
}

Output
GeeksforGeeks

Important Points about memcpy()

The memcpy() function is an old function which C++ inherited from C, so it is to be used with care. Keep in mind the following points:

  1. memcpy() doesn't check for overflow or \0.
  2. memcpy() function copies the memory in a byte-by-byte format without any checks or transformations, meaning it does not handle type conversions or alignment issues.
  3. If the destination already contains some data then memcpy() function simply overwrites the data.
  4. The memcpy() function in C++ creates a shallow copy as it only copies the raw bytes of the memory from one location to another. It does not perform a deep copy or handle higher level objects.
  5. memcpy() cannot handles overlapping memory regions and may lead to undefined behaviour when source and destination addresses overlap.
  6. The memcpy() function simply copies bytes without initialising any memory.
  7. memcpy() only copies the pointer values (i.e., the addresses they hold), not the actual objects or data those pointers reference.

Note: memmove() is another library function that handles overlapping well.


Next Article
Article Tags :
Practice Tags :

Similar Reads