Let's say I have the following:
Code:char *sendBack(char *someText) { char *newText = malloc( sizeof(*newText)); ... ... ... return(newText); free(newText); // will never get reached here newText = NULL; // will never get reached here }
As you can see, I need to "return" something and then free it once I'm done with it. My question is: Since I can't free it here in the sendBack function, can I free it in the calling function? For example, is this legal?:
Something just doesn't look right.Code:void printMe() { char buf[100]; buf = sendBack("blah blah blah"); printf("%s\r\n", buf); free(buf); // is this freeing *newText? buf = 0; }
Any suggestions?