3 ms·
If you are writing good, and highly reliable C, that is meant to run for long period of time, there will be no mallocs() and frees() inside the main loop becaus
by lbrandy 18y ago
If you are writing good, and highly reliable C, that is meant to run for long period of time, there will be no mallocs() and frees() inside the main loop because memory fragmentation can eventually crash your program. You will dynamically allocate and re-use memory outside of the main loop. You will use the stack for temporary creations. You can actually calculate the maximal stack usage, and ensure you have enough space, instead of 10 days into running that malloc() decides to return a null pointer because of fragmentation.
And one (very good) reason you call free() before your program terminates is so that you can use tools to figure out if there are any "real" memory leaks in your code. This way you don't have to wade through piles and piles of "oh that's not a 'real' memory leak because it'll get cleaned up when the program terminates".