+ 1

free memory allocated within an function?

How do you free memory that is malloc'd in a function when the return element is the malloc'd ie. char *disemvowel(const char *str) { int strlgth = strlen(str); char *answer = malloc (sizeof *answer * strlgth); answer[0] = '\0'; int i, j; i = j = 0; while(str[i] != '\0'){ if(str[i] == 'a' || str[i] == 'A' || str[i] == 'e' || str[i] == 'E' || str[i] == 'i' || str[i] == 'I' || str[i] == 'o' || str[i] == 'O' || str[i] == 'u' || str[i] == 'U'){ ++i; } else { answer[j] = str[i]; ++i; ++j; answer[j] = '\0'; } } return answer; }

20th Mar 2022, 10:02 PM
William Owens
William Owens - avatar
10 odpowiedzi
+ 2
char *disemvowel(char *str) { //int strlgth = strlen(str) + 1; char *answer = malloc (strlen(str) + 1); answer[0] = '\0'; int i, j; i = j = 0; while(str[i] != '\0'){ if(str[i] == 'a' || str[i] == 'A' || str[i] == 'e' || str[i] == 'E' || str[i] == 'i' || str[i] == 'I' || str[i] == 'o' || str[i] == 'O' || str[i] == 'u' || str[i] == 'U'){ ++i; } else { answer[j] = str[i]; ++i; ++j; answer[j] = '\0'; } } strcpy(str, answer); free(answer); return str; }
20th Mar 2022, 10:33 PM
rodwynnejones
rodwynnejones - avatar
+ 2
just to add...unless there is a need to keep the original string intact, you can operate on the "str" directly and declare your function as void and not bother with malloc at all.
20th Mar 2022, 11:08 PM
rodwynnejones
rodwynnejones - avatar
+ 1
rodwynnejones gave the best solution. William Owens The OS keeps track of allocated space by its memory address. You could free the returned pointer in the calling code, but you'd be taking a risk that you might forget to free it once it goes outside the function.
20th Mar 2022, 11:13 PM
Brian
Brian - avatar
+ 1
There was a reason to keep the original string intact. The request was just for the function which had a predefined signature. I would have used void fun(const char *source, char *return){....} and not had to deal with it in the function. But thank you both for helping me out you all are great.
20th Mar 2022, 11:18 PM
William Owens
William Owens - avatar
+ 1
William Owens perhaps it would be more robust then to malloc a string in the calling code and pass a copied string that the function could modify in place.
20th Mar 2022, 11:24 PM
Brian
Brian - avatar
+ 1
The API documentation ought to be responsible and warn you when it is up to you to free structs etc. When that occurs, just pass the address they return to you into free(). But I think usually APIs have you create the storage space and pass it in.
20th Mar 2022, 11:47 PM
Brian
Brian - avatar
+ 1
Brian Thanks for the insight.
21st Mar 2022, 12:14 AM
William Owens
William Owens - avatar
0
Another suggestion from discord was: char *fun(args ...){ returned allocated memory char*}; in main char *str = fun(); free(str);
20th Mar 2022, 11:22 PM
William Owens
William Owens - avatar
0
William Owens yes, that suggestion from discord is the risky approach that I mentioned. The malloc is hiding from view that way, especially if the function is defined in a separate source file.
20th Mar 2022, 11:32 PM
Brian
Brian - avatar
0
Brian I agree I would have malloc'd in prior to the call but the challenge was just for the function. It got me to thinking if an API was built that malloc'd memory, how do you free it?
20th Mar 2022, 11:34 PM
William Owens
William Owens - avatar