C 我在程序中找不到内存泄漏

C 我在程序中找不到内存泄漏,c,C,我编写了一些代码,将值存储在数组中。我正在释放释放方法中的内存,但Valgrind仍报告以下内容: ==640== ==640== HEAP SUMMARY: ==640== in use at exit: 6,094 bytes in 33 blocks ==640== total heap usage: 12,040 allocs, 12,007 frees, 24,146,162 bytes allocated ==640== ==640== LEAK SUMMARY: =

我编写了一些代码,将值存储在数组中。我正在释放释放方法中的内存,但Valgrind仍报告以下内容:

==640== 
==640== HEAP SUMMARY:
==640==     in use at exit: 6,094 bytes in 33 blocks
==640==   total heap usage: 12,040 allocs, 12,007 frees, 24,146,162 bytes allocated
==640== 
==640== LEAK SUMMARY:
==640==    definitely lost: 0 bytes in 0 blocks
==640==    indirectly lost: 0 bytes in 0 blocks
==640==      possibly lost: 0 bytes in 0 blocks
==640==    still reachable: 6,094 bytes in 33 blocks
==640==         suppressed: 0 bytes in 0 blocks
==640== Rerun with --leak-check=full to see details of leaked memory
==640== 
==640== For counts of detected and suppressed errors, rerun with: -v
==640== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
我的代码如下。我首先分配一个堆并在1个数组中插入密钥。然后我有一个2D数组(数组中的数组)来保存键。插入后,我想释放内存

我找不到这个记忆还在哪里。有人能帮忙吗?谢谢

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct hashmap
{
    int** value;
    int* key;
    int keylength;
    int x;
    int y;
};

typedef struct hashmap hashmap;

/*
hashmap* hm_initialize();
Dynamically allocates a new hashmap and returns a pointer to it.
*/
hashmap* hm_initialize()
{
    hashmap *hm = malloc(sizeof(hashmap));
    hm->value = (int**)malloc(sizeof(int*));
    hm->value[0] = (int*)malloc(sizeof(int));
    hm->value[0][0] = -999999991;
    hm->key = (int*)malloc(sizeof(int));
    hm->key[0] = -999999991;
    hm->keylength = 1;
    hm->x = 1;
    hm->y = 1;
    return hm;
};

/*
void hm_release(struct hashmap*);
Releases all memory allocated for the given hashmap.
*/
void hm_release(struct hashmap* hm)
{
    free(hm->key);
    for(int x = 0; x<hm->x;x++)
        free(hm->value[x]);
    free(hm->value);
    free(hm);
}

/*
void hm_insert(struct hashmap* hm, int key, int value);
Inserts the key-value pair to the hashmap.
*/
void hm_insert(struct hashmap* hm, int key, int value)
{
    int flagkey = 0;
    int flagvalue = 0;
    int flaglocationkey;
    int flaglocationvalue;
    int valueexists = 0;
    int counter1 = 0;

    for(int x = 0;x<hm->keylength;x++) {
        if(hm->key[x] == key) { //Old Key Exists and is Active
            flagkey = 1;
            flaglocationvalue = x;
        }
    }

    if(flagkey == 0) {
        hm->key = realloc(hm->key,(hm->keylength+1)*sizeof(int));
        hm->key[hm->keylength-1] = key;
        hm->key[hm->keylength] = -999999991;
        hm->keylength = hm->keylength + 1;
        hm->value = (int**)realloc(hm->value,(hm->x+1)*sizeof(int*));
        hm->value[hm->x] = (int*)malloc(sizeof(int));
        hm->value[hm->x][0] = -999999991;
        hm->value[hm->x-1] = (int*)realloc(hm->value[hm->x-1],2*sizeof(int));
        hm->value[hm->x-1][1] = -999999991;
        hm->value[hm->x-1][0] = value;
        hm->x++;
    }

    if(flagkey == 1) {
       printf("FOUND KEY \n");
       printf("CONTENTS OF VAL1: %d \n", hm->value[flaglocationvalue][0]);

        while((hm->value[flaglocationvalue][counter1]) != -999999991) {
            if(hm->value[flaglocationvalue][counter1] == value) {
                 printf("TRUE");
                valueexists = 1;
            }
            counter1++;
        }

        printf("CONTENTS OF VAL2: %d \n", hm->value[flaglocationvalue][counter1]);
        printf("LENGTH %d \n", counter1);

        if(valueexists == 0) {
            hm->value[flaglocationvalue] = (int*)realloc((hm->value[flaglocationvalue]),(counter1+2)*sizeof(int));
            hm->value[flaglocationvalue][counter1] = value;
             hm->value[flaglocationvalue][counter1+1] = -999999991;
        }
    }
}
#包括
#包括
#包括
结构哈希映射
{
整数**值;
int*键;
int键长;
int x;
int-y;
};
typedef结构hashmap hashmap;
/*
hashmap*hm_initialize();
动态分配一个新的hashmap并返回指向它的指针。
*/
hashmap*hm_initialize()
{
hashmap*hm=malloc(sizeof(hashmap));
hm->value=(int**)malloc(sizeof(int*);
hm->value[0]=(int*)malloc(sizeof(int));
hm->value[0][0]=-9999999 1;
hm->key=(int*)malloc(sizeof(int));
hm->键[0]=-9999999 1;
hm->keylength=1;
hm->x=1;
hm->y=1;
返回hm;
};
/*
void hm_发布(struct hashmap*);
释放为给定哈希映射分配的所有内存。
*/
void hm_发布(结构hashmap*hm)
{
免费(hm->key);
对于(int x=0;xx;x++)
自由(hm->值[x]);
自由(hm->价值);
免费(hm);
}
/*
void hm_insert(结构hashmap*hm,int键,int值);
将键值对插入hashmap。
*/
void hm_insert(结构hashmap*hm,int键,int值)
{
int flagkey=0;
int flagvalue=0;
int flaglocationkey;
int-flaglocationvalue;
int valueexists=0;
int计数器1=0;
对于(int x=0;xkeylength;x++){
如果(hm->key[x]==key){//旧密钥存在且处于活动状态
flagkey=1;
flaglocationvalue=x;
}
}
如果(flagkey==0){
hm->key=realloc(hm->key,(hm->keylength+1)*sizeof(int));
hm->key[hm->keylength-1]=键;
hm->key[hm->keylength]=-9999999 1;
hm->keylength=hm->keylength+1;
hm->value=(int**)realloc(hm->value,(hm->x+1)*sizeof(int*);
hm->value[hm->x]=(int*)malloc(sizeof(int));
hm->value[hm->x][0]=-9999999 1;
hm->value[hm->x-1]=(int*)realloc(hm->value[hm->x-1],2*大小(int));
hm->value[hm->x-1][1]=-9999999 1;
hm->value[hm->x-1][0]=值;
hm->x++;
}
如果(flagkey==1){
printf(“找到的密钥\n”);
printf(“VAL1的内容:%d\n”,hm->value[flaglocationvalue][0]);
而((hm->value[flaglocationvalue][counter1])!=-9999999 1){
如果(hm->value[flaglocationvalue][counter1]==值){
printf(“真实”);
valueexists=1;
}
计数器1++;
}
printf(“VAL2的内容:%d\n”,hm->value[flaglocationvalue][counter1]);
printf(“长度%d\n”,计数器1);
如果(valueexists==0){
hm->value[flaglocationvalue]=(int*)realloc((hm->value[flaglocationvalue]),(计数器1+2)*sizeof(int));
hm->value[flaglocationvalue][counter1]=值;
hm->value[flaglocationvalue][counter1+1]=-9999999 1;
}
}
}

您发布的Valgrind输出表明未检测到内存泄漏:

==640==    definitely lost: 0 bytes in 0 blocks
==640==    indirectly lost: 0 bytes in 0 blocks
==640==      possibly lost: 0 bytes in 0 blocks
第四行(下面)并不意味着你泄露了内存。这仅仅意味着你在退出之前没有释放所有的内存这并不构成内存泄漏,因为退出时操作系统会隐式释放程序分配的所有内存

==640==    still reachable: 6,094 bytes in 33 blocks

如果您试图测试hashmap以确保它在退出时释放所有内存,您可能忘记在退出之前调用
hm_release
函数。

您正在发布的Valgrind输出表明未检测到内存泄漏:

==640==    definitely lost: 0 bytes in 0 blocks
==640==    indirectly lost: 0 bytes in 0 blocks
==640==      possibly lost: 0 bytes in 0 blocks
第四行(下面)并不意味着你泄露了内存。这仅仅意味着你在退出之前没有释放所有的内存这并不构成内存泄漏,因为退出时操作系统会隐式释放程序分配的所有内存

==640==    still reachable: 6,094 bytes in 33 blocks

如果您试图测试hashmap以确保它在退出时释放所有内存,您可能忘记在退出之前调用
hm_release
函数。

Valgrind告诉您下一步要做什么:“使用--leak check=full重新运行以查看泄漏内存的详细信息”做三件事:使用-g编译代码,使用--leak check=full运行valgrind,停止发布长时间的完整程序并要求stackoverflow进行挖掘。@Raymond+1,我喜欢人们甚至不立即阅读错误消息的情况,所以…为什么你不连贯?要么从不抛出MalC/Cyroc的返回值(或者使用C++编译器),要么总是抛出。Valgrind告诉你接下来要做什么:“用Reunun以-Curror Chest= =完整地查看泄露的内存的细节”做三件事:用-g编译代码,使用--leak check=full运行valgrind,停止发布长时间的完整程序并要求stackoverflow进行挖掘。@Raymond+1,我喜欢人们甚至不立即阅读错误消息的情况,所以…为什么你不连贯?要么从不抛出MalC/C++的返回值(或者使用C++编译器),而始终强制转换。