1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h>
void mymemcpy(void * dest, const void * src, size_t len) { assert((dest != NULL) && (src != NULL)); char * pdest = (char *) dest; const char * psrc = (const char *) src; if (pdest > psrc && pdest < psrc + len) { for (size_t i = len - 1; i != -1; --i) pdest[i] = psrc[i]; } else { while(len--) *pdest++ = *psrc++; } }
int main() { const char a[] = "hello world"; void *dest = malloc(23); mymemcpy(dest, a, 5); printf("dest is %s\n", dest); mymemcpy(dest+1, dest, 5); printf("dest is %s\n", dest); free(dest); return 0; }
|