Skip to content

Instantly share code, notes, and snippets.

@orangejulius
Created October 5, 2012 23:25
Show Gist options
  • Save orangejulius/3843041 to your computer and use it in GitHub Desktop.
Save orangejulius/3843041 to your computer and use it in GitHub Desktop.
Simple memcpy implementation
#include <stdio.h>
#include <string.h>
void* my_memcpy(void* destination, void* source, size_t num)
{
int i;
char* d = destination;
char* s = source;
for (i = 0; i < num; i++) {
d[i] = s[i];
}
return destination;
}
int main()
{
char str1[]="Humble Bundle is awesome.";
char str2[26];
char str3[10];
void* r1 = my_memcpy(str2, str1, 5);
printf("str2 is %s, r1 is %p\n", str2, r1);
void* r2 = my_memcpy(str2, str1, 26);
printf("str2 is %s, r2 is %p\n", str2, r2);
//This will cause an overflow
//void* r3 = my_memcpy(str3, str1, 26);
//printf("str3 is %s, r3 is %p\n", str3, r3);
return 0;
}
@ideasman42
Copy link

signature should be void* my_memcpy(void* destination, const void* source, size_t num)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment