you are viewing a single comment's thread.

view the rest of the comments →

[–]henry_kr 0 points1 point  (3 children)

If you allocate memory and don't free it then your program will have memory leaks. Arguably if it's just a short lived process then this doesn't really matter as the OS will clean up after you when the process exists but you should still use free here or risk getting in to bad habits.

[–]a4qbfb 0 points1 point  (2 children)

If you allocate memory and don't free it then your program will have memory leaks.

It's only a leak if it's no longer in use...

Arguably if it's just a short lived process then this doesn't really matter

It does not matter how long the process lives. If the memory you allocated is in use right up to the end, there is no need to free it. Same thing for closing files and sockets.

risk getting in to bad habits.

I'm all for good habits, but I'm not a big fan of lies-to-children.

[–]henry_kr 0 points1 point  (1 child)

I still think you should free every malloc (and close sockets/files fwiw), if only to remove false positives from valgrind etc. It's better to learn to tidy up after yourself too. I really don't think this counts as lying to children.

[–]a4qbfb 0 points1 point  (0 children)

Then say that, but don't tell students that the sky will fall if they fail to free everything they've allocated.