在 C++ 中检查内存泄漏的最佳方法是什么?

2021-12-31 00:00:00 memory-leaks c++

我正在实现一个带有链表的稀疏矩阵,手动检查泄漏并不好玩,有什么想法吗?

I'm implementing a sparse matrix with linked lists and it's not fun to manually check for leaks, any thoughts?

推荐答案

valgrind用于 Unix 的分析器提供了不错的泄漏检测.

The valgrind profiler for Unix offers a decent leak detection.

然而,这只是成功方法的一部分.另一部分是防止(即最小化)显式内存处理.智能指针和分配器可以在很大程度上帮助防止内存泄漏.此外,确实使用 STL 类:std::list 已经提供了无泄漏链表实现.

However, this is only one part of a successful approach. The other part is to prevent (i.e. minimize) explicit memory handling. Smart pointers and allocators can help a great deal in preventing memory leaks. Also, do use the STL classes: a leak-free linked list implementation is already provided by std::list.

相关文章