检查数组/二维数组/结构数组/字符串数组是否被释放

Check if array / 2d array / array of structs / array of strings was freed

有什么方法可以检查数组是否已完全释放并且没有留下任何东西? Valgrind 只说有一些未释放的块。调试器或类似的东西是专门为此制作的吗?

所以,不,仅通过查看指针无法知道您是否已释放所有内容。你必须知道你的数据结构和算法,知道什么时候释放分配的内存。

在 Windows (MSC) 下,我在最后使用以下命令检查程序终止时是否释放了所有分配的内存:

// Check heap upon exit: all memory freed and not corrupted?
#include <crtdbg.h>
_CrtMemState memStateStart, memStateEnd, memStateDelta;

int WinMain(...
{
    ...
    // Make a checkpoint of the heap's state so we can later check the heap is still OK
   _CrtMemCheckpoint( &memStateStart );

   ghMainWnd = CreateWindow(                           // Create the app. main window
           ...
   );
   ...
}
...
WndProc(hWnd, msg,...)
{
    ...
            case WM_CLOSE:
                    // Check the heap
                    _CrtMemCheckpoint( &memStateEnd );
                    _CrtSetReportMode( _CRT_WARN,   _CRTDBG_MODE_WNDW );
                    _CrtSetReportMode( _CRT_ERROR,  _CRTDBG_MODE_WNDW );
                    _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_WNDW );
                    if (_CrtMemDifference( &memStateDelta, &memStateStart, &memStateEnd ))
                        _CrtMemDumpStatistics( &memStateDelta );
                    _CrtDumpMemoryLeaks();
                    DestroyWindow (hWnd);
                    return (0);