Free function in C

Hello,

Could you please explain this?

I am confused.
The first sentence told us we don't need free() memory but in the second sentence told us free() memory.

Could you please give me an example?

Thanks in advance

Please see the following image:

http://uupload.ir/files/kobe_29838-screenshot-2020-10-02-133828.jpg
Last edited on
IMO, the first sentence is incorrect, just because some operating systems will reclaim unused memory doesn't you shouldn't "free" memory you manually allocated.

The keys to the second sentence are "when no longer needed, and the program continues to run".

second that, the first sentence is flat out WRONG. While large computer operating systems (windows, big unix, OSX, whatever) and even small but full computers (tablets and phones) will do it for you, this is like having your mom come behind you to put your dirty underwear in the laundry bin. Yes, she may do it for you, but it really should have been your responsibility. And, some systems don't have a mom to do it for you, and in those, if you repeatedly fail to release the assets you grabbed, you eventually crash the computer due to no free memory (or other assets). And, this is good training FOR other assets; many things besides memory operate off a 'take control, use it, release control' model and you should be in the habit of doing this from early on.

an example is pretty easy.

int main()
{
int *ip = malloc(800); //this is 800 bytes, not 800 ints. its likely 100 or 200 depending on int size
} //program ends, mom cleans up the mess you left.

vs

int main()
{
int *ip = malloc(800); //this is 800 bytes, not 800 ints. its likely 100 or 200 depending on int size
free(ip);
} //program ends, you cleaned up your own mess
Last edited on
Topic archived. No new replies allowed.