If I would write:
char *a=malloc(sizeof(char)*4);
a=\"abc\";
char *b=\"abc\";
do I need to free this memory, or is it done by my system?
You've got a memory leak here. When you set a="abc"
, you're not filling the memory you just allocated, you're reassigning the pointer to point to the static string "abc". b
points to the same static string.
What you want instead is strncpy(a, "abc", 4), which will copy the contents of "abc" into the memory you allocated (which a
points to).
Then you would need to free it when finished.