我正在尝试完成一项大学作业,评分标准指定 5% 用于内存管理 - 特别是没有内存泄漏。
据我了解,简单 C 程序中的内存泄漏仅是由程序放弃的指针引起的 - 即,malloc/calloc/etc 调用永远不会有相应的free
。
我的问题分为三个部分:
- Solaris 上最简单的方法是什么
和 OSX 来“证明”你没有
泄漏任何内存吗?
- XCode 有吗
任何有助于确定记忆力的工具
泄漏?
- 有操作系统吗
释放所有先前分配的
一旦ac程序内存
进程结束?
I am trying to complete a college assignment, and the marking criteria specifies 5% for memory management - specifically for having no memory leaks.
As I understand it, memory leaks in simple C programs, are only caused by pointers which have become abandoned by the program - ie, malloc/calloc/etc calls which are never have a corresponding free
.
My question is in 3 parts:
- Whats the simplest way on Solaris
and OSX to 'prove' that you haven't
leaked any memory?
- Does XCode have
any tools to help determine memory
leaks?
- Does the operating system
release all previously allocated
memory within a c program once the
process ends?
发布评论
评论(4)
Valgrind 是你的朋友。
Valgrind is your friend.
一般来说,学习如何在不使用内存调试器之类的工具(早期)的情况下避免泄漏是一个好主意——特别是对于简单的程序。然而,这很痛苦:当涉及到构建任何不平凡的东西时,您将需要开始学习如何使用更高级的调试工具(例如 Valgrind,正如 Alex Reynolds 在另一个答案中建议的那样。)
In general, it's a good idea to learn how to avoid leaks without using tools like a memory debugger (early on) -- especially for your simple programs. It's painful, however: when it comes to building anything non-trivial you'll want to start learning how to use the more advanced debugging tools (like Valgrind, as Alex Reynolds suggested in another answer.)
Mac OS 的答案和要避免的示例(为您节省了半个小时)。
Mac OS 不附带 Valgrind 或 dmalloc。此外,Valgrind 在尝试安装到 Sierra 时存在一些兼容性问题。
有一个名为“leaks”的实用程序,我通过以下方式运行它:
不幸的是,这不报告明显的内存泄漏...也许我用错了它,但我只是在寻找一个检查我的 C 程序是否按应有方式释放其内存的简单方法。
如果您有时间,那么也许阅读并使用在命令行上使用 OSX Leaks 进行 C 程序?
资源:
PS:也许如果与“iprofiler”一起使用,那么它可能会很有用,但我没有安装它。
Answer for Mac OS and an example to be avoided (saved you half an hour).
Mac OS doesn't come with Valgrind or dmalloc. Moreover, Valgrind has some compatibility issues when trying to get it installed in Sierra.
There is utility called "leaks", which I get it running by this:
Unfortunately, this doesn't report obvious memory leaks... Maybe I am using it wrong, but I was just searching for an easy way to check that my C program free'd its memory as it should.
If you have time, then maybe read and use Using OSX Leaks for C Programs on the Command Line?
Resources:
PS: Maybe if used with "iprofiler", then it might be useful, but I didn't had it installed.
还有 dmalloc
there's dmalloc too