查找 C 程序中内存泄漏的最佳方法

发布于 2024-11-07 17:14:14 字数 304 浏览 0 评论 0 原文

我正在尝试完成一项大学作业,评分标准指定 5% 用于内存管理 - 特别是没有内存泄漏。

据我了解,简单 C 程序中的内存泄漏仅是由程序放弃的指针引起的 - 即,malloc/calloc/etc 调用永远不会有相应的free

我的问题分为三个部分:

  1. Solaris 上最简单的方法是什么 和 OSX 来“证明”你没有 泄漏任何内存吗?
  2. XCode 有吗 任何有助于确定记忆力的工具 泄漏?
  3. 有操作系统吗 释放所有先前分配的 一旦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:

  1. Whats the simplest way on Solaris
    and OSX to 'prove' that you haven't
    leaked any memory?
  2. Does XCode have
    any tools to help determine memory
    leaks?
  3. Does the operating system
    release all previously allocated
    memory within a c program once the
    process ends?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鲸落 2024-11-14 17:14:14

Valgrind 是你的朋友。

Valgrind is your friend.

留蓝 2024-11-14 17:14:14
  1. 对于每个 malloc(),您需要确保恰好有一个 free()。
  2. 我没有使用过 XCode,但是论坛条目可能会有所帮助。
  3. 是的。然而,让正在运行的程序“泄漏”仍然是很糟糕的形式。

一般来说,学习如何在不使用内存调试器之类的工具(早期)的情况下避免泄漏是一个好主意——特别是对于简单的程序。然而,这很痛苦:当涉及到构建任何不平凡的东西时,您将需要开始学习如何使用更高级的调试工具(例如 Valgrind,正如 Alex Reynolds 在另一个答案中建议的那样。)

  1. For every malloc(), you need to ensure that you have exactly one free().
  2. I haven't worked with XCode, but this forum entry may help.
  3. Yes. It's still poor form to let your running program 'leak,' however.

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.)

不甘平庸 2024-11-14 17:14:14

Mac OS 的答案和要避免的示例(为您节省了半个小时)。


Mac OS 不附带 Valgrind 或 dmalloc。此外,Valgrind 在尝试安装到 Sierra 时存在一些兼容性问题。

有一个名为“leaks”的实用程序,我通过以下方式运行它:

leaks -atExit --/Contents/Developer/usr/lib/libLeaksAtExit.dylib ./a.out

不幸的是,这不报告明显的内存泄漏...也许我用错了它,但我只是在寻找一个检查我的 C 程序是否按应有方式释放其内存的简单方法。

如果您有时间,那么也许阅读并使用在命令行上使用 OSX Leaks 进行 C 程序?

资源:

  1. 查找内存泄漏
  2. 在 C/C++ 可执行文件上使用“leaks”命令

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:

leaks -atExit --/Contents/Developer/usr/lib/libLeaksAtExit.dylib ./a.out

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:

  1. Finding Memory Leaks
  2. Using the "leaks" command on a C/C++ executable

PS: Maybe if used with "iprofiler", then it might be useful, but I didn't had it installed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文