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.

Pointers and memory leaks in C

21 pointsby MonCalamariover 10 years ago

6 comments

dmitrygrover 10 years ago
Actually, "what bothers [...] the most about C" for me is not pointers or leaks. It is people who blame the language for their inability to keep track of the memory they allocate and use.
评论 #8439635 未加载
wahernover 10 years ago
&quot;Accessing the null pointer is very dangerous, as it might crash your program. Always make sure that you are not accessing null pointers.&quot;<p>If you&#x27;re going to read or write through an invalid pointer, the NULL pointer is your friend, and an immediate segfault is the best thing that could happen. The worse thing that could happen is that the program keeps chugging along and you never notice the bug.<p>&quot;Always use memset along with malloc, or always use calloc&quot;<p>That&#x27;s a good way to defeat checkers like Valgrind. I&#x27;m not saying it&#x27;s a bad idea, but not something a programmer should be instinctively doing without thinking about it. In fact, excepting stylistic stuff, &quot;always&quot; is a poor way to approach programming. You should do nothing which isn&#x27;t deliberate and well thought out.
flohofwoeover 10 years ago
I wonder why the link made it to the front page. Everything in there is absolutely basic knowledge which every programmer who specifically chooses C over other languages for a task knows anyway. If you choose C you know that you have to be careful about dynamic memory. Somewhat obvious, no?
评论 #8439328 未加载
perlgeekover 10 years ago
Sadly it doesn&#x27;t address the debugging problem at all. Mentioning very useful tools like valgrind and address sanitizer (asan), which can help to save lots of debugging time.
larssorensonover 10 years ago
<p><pre><code> char *name = (char *) malloc(11); &#x2F;&#x2F; Assign some value to name memcpy ( p,name,11); &#x2F;&#x2F; Problem begins here </code></pre> &quot;In this example, the memcpy operation is trying to write 11 bytes to p, whereas it has been allocated only 10 bytes.&quot;<p>I&#x27;m sorry, but I see it as having malloc&#x27;d 11 bytes...
评论 #8439420 未加载
galapagoover 10 years ago
&gt; memset(p,’\0’,10);
评论 #8439088 未加载