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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With