Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t在C中释放一些内存_C_Memory_Valgrind - Fatal编程技术网

Can';t在C中释放一些内存

Can';t在C中释放一些内存,c,memory,valgrind,C,Memory,Valgrind,我正在开发一个哈希表C程序。我在以下函数中只有1个内存泄漏: void put(char *key, char *value, TD* H) { if(!get(key, H)) { int poz = fd(key, H->M); CelulaH *aux, *aux2; aux = malloc(sizeof(CelulaH)); aux->key = malloc(50); aux-

我正在开发一个哈希表C程序。我在以下函数中只有1个内存泄漏:

void put(char *key, char *value, TD* H)
{
    if(!get(key, H))
    {
        int poz = fd(key, H->M);
        CelulaH *aux, *aux2;
        aux = malloc(sizeof(CelulaH));
        aux->key = malloc(50);
        aux->value = malloc(50);
        strcpy(aux->key, key);
        strcpy(aux->value, value);

        if(H->v[poz] == NULL)
        {
            H->v[poz] = (TCelulaG*)malloc(sizeof(TCelulaG));
            H->v[poz]->info = malloc(sizeof(CelulaH));
            memcpy(H->v[poz]->info, aux, sizeof(CelulaH));
            H->v[poz]->urm = NULL;
        }
        else
            InsLGO(&H->v[poz], aux, sizeof(CelulaH), cmp);

        //if(aux)
            free(aux);
    }
}
我有三种结构:TD、CelulaH、TCelulaG。这是他们的样子:

typedef struct celula
{
    struct celula* urm;
    void* info;
} TCelulaG, *TLG, **ALG;


typedef struct
{
    size_t M;
    TFhash fd;
    TLG *v;
} TD;


typedef struct
{
    char *key, *value;
} CelulaH;  
这是Valgrind的输出:

==5380== Conditional jump or move depends on uninitialised value(s)
==5380==    at 0x8048A2B: get (in /home/luzi/TemaSD/tema1)
==5380==    by 0x8048B26: put (in /home/luzi/TemaSD/tema1)
==5380==    by 0x8048E19: CitireComenzi (in /home/luzi/TemaSD/tema1)
==5380==    by 0x804909E: main (in /home/luzi/TemaSD/tema1)
==5380== 
==5380== Conditional jump or move depends on uninitialised value(s)
==5380==    at 0x8048BB7: put (in /home/luzi/TemaSD/tema1)
==5380==    by 0x8048E19: CitireComenzi (in /home/luzi/TemaSD/tema1)
==5380==    by 0x804909E: main (in /home/luzi/TemaSD/tema1)
==5380== 
==5380== 50 bytes in 1 blocks are definitely lost in loss record 1 of 2
==5380==    at 0x402A17C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==5380==    by 0x8048B60: put (in /home/luzi/TemaSD/tema1)
==5380==    by 0x8048E19: CitireComenzi (in /home/luzi/TemaSD/tema1)
==5380==    by 0x804909E: main (in /home/luzi/TemaSD/tema1)
==5380== 
==5380== 50 bytes in 1 blocks are definitely lost in loss record 2 of 2
==5380==    at 0x402A17C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==5380==    by 0x8048B73: put (in /home/luzi/TemaSD/tema1)
==5380==    by 0x8048E19: CitireComenzi (in /home/luzi/TemaSD/tema1)
==5380==    by 0x804909E: main (in /home/luzi/TemaSD/tema1)
==5380== 
所以现在唯一的问题似乎是“put”函数中的两个malloc。该计划中的所有其他Malloc都是免费的,所以这是唯一可以免费使用的。有什么想法吗

aux = malloc(sizeof(CelulaH));
aux->key = malloc(50);
aux->value = malloc(50);
然后你需要把它们全部释放出来

为了克服内存泄漏,您需要
释放()
所有内存 使用
malloc()
calloc()
realloc()


结构也可以在malloc语句中使用

您可以在如下结构上分配内存:

/* Allocate memory */
aux = (CelulaH*)malloc(sizeof(CelulaH));
/* Use variables */
strcpy((*aux).key, key);
strcpy((*aux).value, value);
然后释放它

free(aux);

我已经试过了,但在多次调用“put函数”时,它会变成双重免费或损坏。我需要设法只在最后一次通话时释放,以便释放备忘录,在备忘录中我将“键”和“值”堆叠起来。我认为问题在于我糟糕的编码风格。“我只有一个内存泄漏”。。。你知道的!
free(aux);