char* oldstr = (char*)malloc(sizeof(str));
strcpy(oldstr, str);
You don't allocate enough space. Since str
is a char *
, you are allocating however many bytes a char *
takes on your platform, which is probably not enough to hold a string. You want:
char* oldstr = malloc(strlen(str)+1);
strcpy(oldstr, str);
Or, for simplicity:
char* oldstr = strdup(str);