TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

What do you mean by “Memory Management”?

27 点作者 Decabytes大约 2 年前

3 条评论

msla大约 2 年前
<p><pre><code> Unfortunately, the * in the *pointer_my_var syntax is pulling double duty here as it is used both to dereference a pointer as well as declare a pointer. </code></pre> This isn&#x27;t unfortunate: It&#x27;s a rule in C that declaration should mirror use, that a variable&#x27;s declaration should look like that variable&#x27;s use. This is more obvious when declaring function pointers:<p><pre><code> int (*cmp)(char *, char *); </code></pre> which declares cmp as a pointer to a function which takes two pointers-to-char and returns an int, and obviously cmp is used like:<p><pre><code> res = cmp(foo, bar); </code></pre> What&#x27;s unfortunate is how it&#x27;s impossible to escape asterisks in normal text around here.<p>Also: Don&#x27;t cast the return values of malloc and friends. It can mask a failure to include stdlib.h, which means the compiler considers the functions to return an int, which is half the size of a pointer on most modern systems, so your pointer just got mangled. Yes, implicit int is Officially Bad in the modern standard. How much do you want to bet your compiler is strict enough to catch this?
评论 #35292785 未加载
dragontamer大约 2 年前
In practice, memory is the largest pool of resources a programmer has to manage. In the 1960s, a computer may only have 1000-words or 2000-words of memory, and you could manage it all manually. Memory location#45 means blah, Memory location#80 means bar, etc. etc.<p>This form of variable-allocation is now done automatically (ex: int foobar; will automatically manage a memory location to be representing a 4-byte value named foobar). This is the simplest way of managing memory (even on extremely constrained modern platforms like ATTiny4, which only has 256 Bytes of RAM. BTW: I do suggest people play with microcontrollers, if only to improve your skills at programming in constrained environments).<p>But as you start having to efficiently manage more than that: a million bytes (aka: 1MB), or a billion bytes (aka: 1GB), more complex patterns arise. In particular, a program may not know how much memory it needs at compile time, and may need to shift the amount of memory usage arbitrarily upon the user&#x27;s work pattern.<p>For example: lets say you are writing a web-client, and your user is going to visit the webpage www.google.com. How much memory should you allocate for it? Well, you don&#x27;t know. You have to start downloading the page before you even know how much memory it will use. (Upon talking to www.google.com, the Google Server says in the HTTP protocol how many bytes the webpage will take up, giving the program an opportunity to allocate memory at that point). To properly store the webpage, you need a memory-management scheme.<p>-------------<p>Modern languages have two patterns for more complex memory management like the &quot;browse to www.google.com&quot; example I described above.<p>1. C&#x27;s malloc-and-free pattern. The programmer reserves memory with malloc or calloc. And the programmer says that they&#x27;re done using the memory with free(), allowing a future malloc() call to recycle the memory.<p>2. Garbage collection, aka malloc-only. The system automatically determines when memory &quot;should be freed&quot;. Though more complex to program, its been popular even as early as the 1970s. It turns out that programmers are pretty bad at writing free() correctly in practice.<p>There&#x27;s also the &quot;automatic&quot; memory management implemented by the compiler. This is called &quot;stack allocation&quot; in most cases, and IMO is important to understand how it works... if only because of how efficiently it recycles the L1 Cache and speeds up your program. But we can talk about that some other time!
评论 #35292637 未加载
_trackno5大约 2 年前
I don&#x27;t get the point of this article.<p>All it did was cover some C standard library functions and say that what a GC does is manage the memory lifecycle in languages like Python.<p>What did other people take from this?
评论 #35291817 未加载
评论 #35292256 未加载
评论 #35291756 未加载
评论 #35292378 未加载