C and the dangers of memcpy()

Updates:

  • 2019-08-12 – Added note about using unions.

In the C programming language, memcpy (memory copy) is a function used to copy a range of bytes from one location in memory to another. I have used it often. Today, my boss mentioned something about not liking memcpy() because of all the dangers of using it. I understand that incorrect addresses, bad calculations, and simple omissions from a copy/paste can cause a memcpy() to have bad results. But, what’s the alternative?

He mentioned that he prefers to use C structures to copy data around, as opposed to using memcpy() to copy blocks of data.

Instead of doing this…

char Buffer1[64];
char Buffer2[64];

// Load Buffer1 with something.
strncpy (Buffer1, "Copy this string over", 64);

// Copy Buffer1 into Buffer2.
memcpy (Buffer2, Buffer1, 64);

That’s a simple, straight-forward example that seems hard to mess up. But doing something like this would be impossible to mess up:

typedef struct
{
    char Buffer[64];
} CopyStruct64;

CopyStruct64 Buffer1;
CopyStruct64 Buffer2;

// Load Buffer1 structure with something.
strncpy (Buffer1.Buffer, "Copy this string over", 64);

// Copy Buffer1 structure into Buffer2 structure.
Buffer2 = Buffer1;

Interesting. With properly defined structures, copying a block of data from one place to the other is done using code generated by the compiler.

(Update) And it seems I misspoke. He was actually talking about using unions, not structures. I’ve used unions as unions, but never for something like this. I did a similar test. (I realize there are several ways to declare a union, but I am using typedef so it looks similar to the above structure example.)

typedef union
{
    char Buffer[64];
} CopyUnion64;

CopyUnion64 Buffer1;
CopyUnion64 Buffer2;

// Load Buffer1 structure with something.
strncpy (Buffer1.Buffer, "Copy this string over", 64);

// Copy Buffer1 structure into Buffer2 structure.
Buffer2 = Buffer1;

Using union appears to work the same way, and since my example was not using structure elements, it seems to make more sense to do it that way. (End of Update)

I’m just now wrapping my brain around this, trying to figure out how this approach could be used to avoid using memcpy() and pointers.

What do you think? Am I on the right track to what he was talking about?

Until next time…

3 thoughts on “C and the dangers of memcpy()

  1. Zac

    Am I wrong to say that

    // Copy Buffer1 structure into Buffer2 structure.
    Buffer2 = Buffer1;

    Isn’t copying any data? It just reassigns Buffer1 ptr to Buffer2. So if you changed Buffer2 you would also be changing Buffer1?

    Reply
    1. Allen Huffman Post author

      Here’s a test…


      #include <stdio.h>
      #include <stdlib.h>

      typedef struct
      {
      int a;
      int b;
      int c;
      } MyStruct;

      void ShowStruct(MyStruct s)
      {
      printf ("\n");
      printf ("a = %d\n", s.a);
      printf ("b = %d\n", s.b);
      printf ("c = %d\n", s.c);
      }

      int main()
      {
      MyStruct one;
      MyStruct two;

      one.a = 100;
      one.b = 200;
      one.c = 300;

      two.a = 123;
      two.b = 345;
      two.c = 678;

      ShowStruct(one);
      ShowStruct(two);

      printf ("two = one:\n");

      two = one;

      ShowStruct(one);
      ShowStruct(two);

      return EXIT_SUCCESS;
      }

      Running this prints:

      a = 100
      b = 200
      c = 300

      a = 123
      b = 345
      c = 678
      two = one:

      a = 100
      b = 200
      c = 300

      a = 100
      b = 200
      c = 300

      Reply

Leave a Reply to Allen HuffmanCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.