In your situation, you will have no way to free dynamically allocated memory, because you lose the link to it.
Try the following:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *a=(char*)malloc(sizeof(char)*4);
printf("Before: %p\n",a);
a = "abc";
printf("After: %p\n",a);
free(a);
char *b = "abc";
return 0;
}
You'll get
Before: 0x100100080
After: 0x100000f50
You will see that the two pointers are different from each other. This is because the string literal "abc"is placed in the binary data sector and when you execute
a = "abc"
a "abc", . free on a , , . ,
strncpy(a, "abc", 4)
, .