TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Memory leak proof every C program

8 pointsby iio717 days ago

3 comments

musicale17 days ago
&gt; If you don’t call free, memory usage will increase over time, but technically, it’s not a leak<p>Prevent &quot;technical&quot; memory leaks while creating actual memory leaks.<p>Also what happens when saver is zero due to a failed malloc?
jll2917 days ago
It&#x27;s a clever idea to keep track of the chunks in your own version of malloc(), like some others have proposed own flavors of allocators for other purposes - e.g. with sentinels at the beginning and end of buffers to detect overruns; but I&#x27;m not convinced it solves all cases of leaks.<p>The OP essentially models within the C program what the operating system&#x2F;C runtime already does for the C program - that&#x27;s why we don&#x27;t _need_ to call free() if we don&#x27;t want to, we can just leave main or call exit() and everything will be nicely cleaned up by the OS without taking extra measures (the ISO C standard and POSIX guarantee that).<p>&gt; Even if no other references to the pointer exist in the program, the pointer has not leaked.<p>IMHO, this is a misunderstanding. I would still call this a memory leak, because the ordinary code (the actual code that requested the particular block) lost control of it. Furthermore, how do you realize whether a particular block in the list of allocated chunks can be freed or not? (And once you hypothetically added such a detection function, you&#x27;re well on your way to implement what is called a mark-and-sweep garbage collector.)
abstractspoon17 days ago
I don&#x27;t think this was intended to be taken seriously