C/C++ memcpy()- memory copy

www.‮tual‬turi.com
C/C++ memcpy()- memory copy

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

syntax

The memcpy() function takes two memory address locations (src and dst) as arguments, as well as the number of bytes to copy (n).

Since C/C++ typically uses void* pointers to represent memory location addresses, calls to source and destination addresses are "voidsrc" and "voiddst".

The function returns a pointer to the target memory address "dst", but in practice, we usually don't capture the return value. (because we already have the pointer)

syntax:

#include <string.h>

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

The header file '<string. h>' is required for loading this library function.

parameter:

  • dst->Pointer to the destination address
  • src->Pointer to the source address
  • n->The number of bytes to copy

Return value

This returns the pointer to the target location ('dst').

Note: To avoid any type of overflow, the array pointed to by the destination and source parameters must be at least n bytes in size and cannot overlap.

Examples

Here is an example of using 'memcpy()' to copy strings:

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

int main () {
  //This is constant, since memcpy() will not modify this
 const char src[50] = "lautturi";
 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: lautturi

use memcpy()to append string to another one

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

int main() {
  //This is constant, since memcpy() will not modify this
  const char src[100] = "This is a lautturi 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;
}

output:

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

A simple memcpy() implementation

This is a simple implementation of 'memcpy()' in C/C++ that attempts to replicate some of the mechanisms of the function.

We first convert the 'src' and 'dst' types to 'char *' pointers because we can't dereference 'void *' pointers.
The "void*" pointer is only used to transfer data between functions, threads, but not to access them.

Now we can copy the data directly 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 you will get the same output by replacing 'memcpy()' with a new function.

Created Time:2017-08-28 06:27:09  Author:lautturi