c - when to carefully use free() to free up malloc() used memory? -


i read many question here in , other articles regarding free() function in c frees memory of unused variables. in case, have following code block.

char *injectstrat(char *str, char *strtoin, int pos) {     char *strc = malloc(strlen(str) + strlen(strtoin) + 1);     strncpy(strc, str, pos);     strc[pos] = '\0';     strcat(strc, strtoin);     strcat(strc, str + pos);     return strc; } 

the above function use inject string block in array. using malloc create new char*. in above case need free(strc) ? advice pls.

strc return value of function, don't call free(strc) inside function itself. need free outside function, time when string not used anymore.


Comments