Main:
char *tmpip;
tmpip = get_public_ip();
function get_public_ip:
char * get_public_ip(void){
char *ipresult;
if(((ipresult = malloc(17))) == NULL){
perror("malloc on pub ip");
exit(1);
}
if(fgets(ipresult, 16, tmpfp) == NULL){
perror("fgets error pubip");
exit(1);
}
fclose(tmpfp);
return ipresult;
}
My question is:
it is good to do inside the main free(tmpip) or it is wrong?
It’s a good way of coding: you
malloc()some memory in a function andfree()it when not needed anymore. Be sure to comment your function or prototype that it willmalloc()the memory needed, so you know that you have tofree()it.