When shall I free my unsigned char* if I need to pass the unsigned char* into a function?
Example
void myFunction(unsigned char *request){
// do I need to use free(request); here?
}
int main (){
// main code
unsigned char *request = NULL;
// read from buffer and reallocate memory (using realloc & memcpy)
myFunction(request); // I do not want to read or use "request" after this function call.
free(request); // is this ok? Or should this be inside myFunction also?
return 0;
}
Use
free()on something as soon as you’re done using it. So for instance, you probably wouldn’t do it insidemyFunction()because you’re probably still going to be interested in the value pointed at byrequestwhen you exit that function back tomain.