Dan Luu 写系统问题时,习惯先测量、再对照、最后才下结论。把「Malloc tutorial」放到智能体、评测和线上系统里,真正要问的是:默认做法会不会系统性失败。下面用中文整理成可执行的工程笔记,去掉原站导航和无关链接。
问题怎么暴露
Let's write a malloc and see how it works with existing programs!
This is basically an expanded explanation of what I did after reading this tutorial by Marwan Burelle and then sitting down and trying to write my own implementation, so the steps are going to be fairly similar. The main implementation differences are that my version is simpler and more vulnerable to memory fragmentation. In terms of exposition, my style is a lot more casual.
测量时容易踩的坑
This tutorial is going to assume that you know what pointers are, and that you know enough C to know that *ptr dereferences a pointer, ptr->foo means (*ptr).foo , that malloc is used to dynamically allocate space , and that you're familiar with the concept of a linked list. For a basic intro to C, Pointers on C is one of my favorite books. If you want to look at all of this code at once, it's available here .
Preliminaries aside, malloc's function signature is
对照之后能下的结论
void *malloc(size_t size); It takes as input a number of bytes and returns a pointer to a block of memory of that size.
There are a number of ways we can implement this. We're going to arbitrarily choose to use sbrk . The OS reserves stack and heap space for processes and sbrk lets us manipulate the heap. sbrk(0) returns a pointer to the current top of the heap. sbrk(foo) increments the heap size by foo and returns a pointer to the previous top of the heap.
落到生产里的动作
If we want to implement a really simple malloc, we can do something like
#include <assert.h> #include <string.h> #include <sys/types.h> #include <unistd.h> void *malloc(size_t size) { void *p = sbrk(0); void *request = sbrk(size); if (request == (void*) -1) { return NULL; // sbrk failed. } else { assert(p == request); // Not thread safe. return p; } } When a program asks malloc for space, malloc asks sbrk to increment the heap size and returns a pointer to the start of the new region on the heap. This is missing a technicality,
值得单独记下的观察
- Our malloc is really wasteful if we try to re-use an existing block and we don't need all of the space. Implement a function that will split up blocks so that they use the minimum amount of space necessary
- Find bugs in the existing code! I haven't tested this much, so I'm sure there are bugs, even if this basically kinda sorta works.
落地时建议先做的 5 件事
- 用自己的真实负载测,而不是只用公开榜或厂商数字。
- 把评测设计成能抓到失败模式:平均分好看但尾部崩溃,仍然算失败。
- 智能体默认不会好好用测试;要写进流程,而不是写在口头规范里。
- 性能和正确性都要有基线,改模型或改语言前后必须能对比。
- 结论写成可回滚的决策:哪一版配置、哪一版评测集、谁签字。
和智能体产品怎么接
龙虾PRO做 OpenClaw 落地时,同样吃「先测量再扩面」这条纪律:技能、数字员工和网关都要有可复现评测,而不是只看一次演示通过。
本文侧重全链路风控方法论。落地时请用自身业务单据做回放验证,不要把示例阈值直接当生产策略。 相关:风控体检 · 方案资源
常见问题 FAQ
什么是AI智能系统?
「AI智能系统」可概括为:Let's write a malloc and see how it works with existing programs! 本文从定义、方法与实践要点展开说明。
为什么要关注AI智能系统?
关注AI智能系统,是因为它直接影响效率、风险与可复制性。文中指出:Let's write a malloc and see how it works with existing programs!
如何落地AI智能系统?有哪些关键步骤?
建议按以下路径推进AI智能系统:1) Find bugs in the existing code! I haven't tested this much, so I'm su…;2) 用自己的真实负载测,而不是只用公开榜或厂商数字。;3) 把评测设计成能抓到失败模式:平均分好看但尾部崩溃,仍然算失败。;4) 智能体默认不会好好用测试;要写进流程,而不是写在口头规范里。;5) 性能和正确性都要有基线,改模型或改语言前后必须能对比。。细节见正文对应章节。
AI智能系统适合哪些人或团队?
AI智能系统更适合:产品/技术负责人、运营与增长团队、需要落地智能体或自动化的中小团队、关注「AI智能系统」方向的读者。若你只需要单次聊天式问答,可先读概念;若要上生产,请重点看步骤、权限与风控相关段落。
关于「问题怎么暴露」,本文给出了什么结论?
在「问题怎么暴露」部分,要点是:ing down and trying to write my own implementation, so the steps are going to be fairly similar. The main implementation differences are that my version is simpler and more vulnerable to memory fragmentation. In terms of
关于「测量时容易踩的坑」,本文给出了什么结论?
在「测量时容易踩的坑」部分,要点是:ally allocate space , and that you're familiar with the concept of a linked list. For a basic intro to C, Pointers on C is one of my favorite books. If you want to look at all of this code at once, it's available here .