Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does memcpy need a const void pointer?

Tags:

c

I wanted to use memcpy to do a 'type agnostic swap' in an array which might contain arbitrary types. Why does it require a const src pointer?

I wrote my own version instead:

void copyBytes(char *x, char *y, int howMany){
  int i;
  for(i = 0; i<howMany; i++){
    *(x+i) = *(y+i);
  }
}

Is there something wrong with my version?

like image 676
Clayton Rabenda Avatar asked Jun 10 '26 19:06

Clayton Rabenda


1 Answers

Is there something wrong with my version?

I'd say so. Here's a critique.

void copyBytes(char *x, char *y, int howMany)

First of all your pointers are char *, which means any pointer type other than char * needs to be explicitly converted. You should be using void *, to which pointer types are implicitly converted.

uint16_t a, b;

copyBytes(&a, &b, sizeof(a));  // &a and &b are uint16_t*, not char*.

Second, the source, y, is not const, which means you will get warnings if you pass a const source.

char buf[512];
const char str[] = "Hello world";  // Contents of string are constant.
copyBytes(buf, str, sizeof(str));  // With your code, this produces a warning.

Third, howMany is signed, meaning you can pass a negative value.

I would recommend a signature like this (incidentally this is very similar to memcpy):

void copyBytes(void *x, const void *y, size_t howMany)

A fourth critique ... libc's memcpy is likely to be much better optimized, using larger-than-byte units, platform-specific performance tricks (example: inline assembly, SSE on x86), etc. There's also memmove which has better specified behavior when the buffers overlap.

In conclusion: It's fine to write these routines yourself for learning purposes, but you're usually much better off with the C library.

like image 62
asveikau Avatar answered Jun 12 '26 10:06

asveikau