So i'm trying to optimize the managment of my memory in my code.
Here's a sample of the code :
Image image;
int main(int argc, char *argv[])
{
image = (Image) malloc(sizeof (struct image));
image = doSomething(image);
}
Image doSomething(Image imageInput) {
Image imageResult;
imageResult = (Image) malloc(sizeof (struct image));
//Code does something here
return imageResult;
}
When is it proper to use the free(); in my example ?
Image image;
int main(int argc, char *argv[])
{
image = (Image) malloc(sizeof (struct image));
image = doSomething(image);
free(image);
}
Image doSomething(Image imageInput) {
Image imageResult;
imageResult = (Image) malloc(sizeof (struct image));
//Code does something here
free(imageInput);
return imageResult;
}
The only time i can see is the "imageInput" variable that is supossed to be copied in the function and is suppose to be erased after the function ends.
Is it overkill to free a function variable ?
And at the end of the execution of the application too.