That's perfect, as long as you're super sure the caller is going to call free to avoid memory leaks .. That's not a big issue on small programs, but when the program gets complex, believe me, you'll be concerned about much more things than freeing a pointer from a supposed-to-be self-contained function ..
But (hands down), there's a much more satisfying solution to this that the standard C Library itself uses. Use a Buffer ! (Claps, Claps)
You know, there is a reason the fgets function for example requires you to provide a character pointer as the first argument, so that it can write to it rather than returning a malloc'd pointer ..
For example ..
#include <ctype.h>
#include <string.h>
void toLower(char *buf, const char *s) {
for(int i = 0; s[i]; ++i)
buf[i] = tolower(s[i]);
}
int main(int argc, const char ** argv) {
const char *s = "ThAt'S a BiG sTrIng";
char lower_version[strlen(s)];
toLower(lower_version, s);
printf("Original Version: %s\nLower Version: %s\n\tTada !\n", s, lower_version);
}
That way, you don't have to worry how you're going to handle the variable in later uses ..
You're leaving this problem to the function caller to deal with.
free().mallocandfreeare in the same DLL. MS run time libraries don't like memory allocated in one DLL to be freed in another DLL.char *newstr = malloc(4 * sizeof *newstr);