I know, any dynamically allocated memory MUST be freed at the end of its use, with free()
.
No. Not freeing has the result that less memory remains for your program to allocate for other purposes. It may (or may not) cause the program's memory footprint to be larger than it otherwise would be. In flagrant, extended, or extreme cases (as judged in an environment-dependent manner), it may mean that insufficient (virtual) memory is available to serve other applications, or to serve the leaky one itself. But none of those things mean you must free memory. You always have the alternative of accepting the consequences of not doing so.
For the most part, the minor advantages attending wanton disregard for freeing allocated memory in no way balance the negative impact on the program and on the system on which it runs, but there are cases in which it does not cause any practical problem. All major operating systems in use today release processes' allocated memory at process termination. Therefore, small leaks in short-running programs are fairly inconsequential on most systems you're likely to meet, and whether one should free memory that one needs to use until just prior to program termination is a matter of style, not practicality.
And that brings us directly to your example program.
However, is it an idiomatic function? If it exists, is it then considered a minor problem not to free the memory in this style of case?
I think you meant "if it exits". As discussed above, there is substantially no difference in practical impact on the program or system between freeing manually just before exiting the top-level call to main()
on one hand, and leaving it to the OS to clean up.
I would like to know if such a function can be used without worrying about freeing the memory every time, although I doubt it.
You stand in grave danger of causing trouble for yourself and others if you neglect to free allocated memory without giving careful consideration to whether doing so will produce problems. You may under some circumstances go without freeing, but it would be foolish to do so without worrying about it, so to speak. Moreover, at your apparent level of experience, you may not yet be equipped to make good judgements in this area.
I would actually put it the other way around, then: being sure to free allocated memory when you no longer need it is the worry-free alternative. Or at least worry-less. You can make that relatively easy by cultivating programming practices that support it.