C/C++ memcpy() – Copy across memory locations

Filed Under: C Programming
memcpy() function

The memcpy() function in C/C++ is used to copy data from one memory location to another. This is a common way of copying data using pointers.

Let’s understand how we can use this function in this article.


Syntax

The memcpy() function takes in two memory address locations (src and dst) as arguments, along with the number of bytes (n) to be copied.

Since C/C++ typically uses a void* pointer to denote memory location addresses, the invocation for the source and destination addresses are void* src and void* dst.

The function returns a pointer to the destination memory address dst, but in practice, we do not usually capture the return value. (Since we already have the pointer with us)

Format:

#include <string.h>

void* memcpy(void* dst, const void* src, size_t n)

The header file <string.h> is necessary to load this library function.

Arguments:

  • dst -> Pointer to the destination address
  • src -> Pointer to the source address
  • n -> Number of bytes to be copied

Return Value

This returns the pointer to the destination location (dst).

NOTE: To avoid any kind of overflow, the size of the arrays pointed to by both the destination and source arguments must be at least n bytes and cannot overlap.

Let’s understand the function using some examples.


Some Examples

Here is an example which copies a string from one location to another using memcpy().

#include <stdio.h>
#include <string.h>

int main () {
    // This is constant, since memcpy() will not modify this
   const char src[50] = "JournalDev";
   char dest[50];

   strcpy(dest, "Sample");
   printf("Before memcpy(), dest: %s\n", dest);

   // It is strlen(src) + 1 since we also copy the null terminator '\0'
   memcpy(dest, src, strlen(src)+1);
   printf("After memcpy(), dest: %s\n", dest);
   
   return(0);
}

Output

Before memcpy(), dest: Sample
After memcpy(), dest: JournalDev

Here is another example which appends a string using memcpy() using appropriate offsets.

#include <stdio.h>
#include <string.h>

int main() {
    // This is constant, since memcpy() will not modify this
    const char src[100] = "This is a JournalDev article.";

    char dst[100] = "memcpy";
    
    printf("Before memcpy(), dst is: %s\n", dst);
    
    // Set offsets for both src and dst
    size_t offset_src = 0, offset_dst = 0;
    offset_src += 4;
    offset_dst += strlen(dst);

    memcpy(dst + offset_dst, src + offset_src, strlen(src));

    printf("After memcpy(), dst is: %s\n", dst);

    return 0;
}

The output will be the destination string appended with ” is a JournalDev article“.

Output

Before memcpy(), dst is: memcpy
After memcpy(), dst is: memcpy is a JournalDev article.

A Simple memcpy() Implementation

Here is a simple implementation of memcpy() in C/C++ which tries to replicate some of the mechanisms of the function.

We first typecast src and dst to char* pointers, since we cannot de-reference a void* pointer. void* pointers are only used to transfer data across functions, threads, but not access them.

Now we can directly copy the data byte by byte and return the void* destination address.

void* memcpy_usr(void* dst, const void* src, size_t n) {
    // Copies n bytes from src to dst
    
    // Since we cannot dereference a void* ptr,
    // we first typecast it to a char* ptr
    // and then do the copying byte by byte,
    // since a char* ptr references a single byte
    char* char_dst = (char*) dst;
    char* char_src = (char*) src;

    for (int i=0; i<n; i++) {
        *char_dst++ = *char_src++;
    }

    return dst;
}

You can verify that by replacing memcpy() with our new function, we will get the same output.


Conclusion

In this article, we learned about the memcpy() library function in C/C++, which is useful to copy data from a source address to a destination address.


References


Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages