Release at the end main()would be right, yes. However, you might think about the null termination of this line. A more idiomatic design, so to speak, refers to all memory management “on the same level”. Sort of:
void foo(char *p)
{
p[0] = 'a';
p[1] = 'b';
p[2] = 'c';
p[3] = '\0';
}
int main(int argc, char **argv)
{
char *p2 = malloc(4);
foo(p2);
printf("%s", p2);
free(p2);
return 0;
}
source
share