29. Valgrind performs new/malloc memory check

Basic idea: Indulge in valgrind and can't get rid of it, review the memory application and release methods of new/malloc;

#include<iostream>
#include<stdio.h>
#include<malloc.h>
using namespace std;
#define N 10
int main()
{

        int *a=new int[N];
        for(int i=0;i<N;i++)    {
                a[i]=i;
        }
        delete[] a;
        /
        int *b=(int*)malloc(sizeof(int)*N);
        for(int i=0;i<N;i++){
                b[i]=i;
        }
        free(b);
        

        int **c=new int*[N];
        for(int i=0;i<N;i++){
                c[i]=new int[N];
                for(int j=0;j<N;j++){
                        c[i][j]=i+j;
                }
        }

        for(int i=0;i<N;i++)    {
                if(NULL!=c[i]){
                        delete[] c[i];
                        c[i]=NULL;
                }
        }
        delete[] c;

        ///

        int  **d= (int**)malloc(sizeof(int*)*N);
        for(int i=0;i<N;i++){
                d[i]=(int*)malloc(sizeof(int)*N);
                for(int j=0;j<N;j++){
                        d[i][j]=i+j;
                }
        }
        for(int i=0;i<N;i++){
                if(NULL!=d[i]){
                        free(d[i]);
                        d[i]=NULL;
                }
        }
        free(d);

        /

        return 0;
}

Memory check:
 

ubuntu@ubuntu:~/test$ g++ -g t.cpp
ubuntu@ubuntu:~/test$ G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind --tool=memcheck --leak-check=full --leak-resolution=high --num-callers=20 --show-reachable=yes --log-file=a.log  ./a.out
ubuntu@ubuntu:~/test$ cat a.log
==32770== Memcheck, a memory error detector
==32770== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==32770== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==32770== Command: ./a.out
==32770== Parent PID: 37391
==32770==
==32770==
==32770== HEAP SUMMARY:
==32770==     in use at exit: 72,704 bytes in 1 blocks
==32770==   total heap usage: 25 allocs, 24 frees, 73,744 bytes allocated
==32770==
==32770== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==32770==    at 0x4C2DBF6: malloc (vg_replace_malloc.c:299)
==32770==    by 0x4EC3EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==32770==    by 0x40106C9: call_init.part.0 (dl-init.c:72)
==32770==    by 0x40107DA: call_init (dl-init.c:30)
==32770==    by 0x40107DA: _dl_init (dl-init.c:120)
==32770==    by 0x4000C69: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==32770==
==32770== LEAK SUMMARY:
==32770==    definitely lost: 0 bytes in 0 blocks
==32770==    indirectly lost: 0 bytes in 0 blocks
==32770==      possibly lost: 0 bytes in 0 blocks
==32770==    still reachable: 72,704 bytes in 1 blocks
==32770==         suppressed: 0 bytes in 0 blocks
==32770==
==32770== For counts of detected and suppressed errors, rerun with: -v
==32770== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

 

Guess you like

Origin blog.csdn.net/sxj731533730/article/details/107574520