Memory Valgrind内存错误(大小为24 free';d的块中有0个字节)

Memory Valgrind内存错误(大小为24 free';d的块中有0个字节),memory,memory-leaks,malloc,valgrind,Memory,Memory Leaks,Malloc,Valgrind,我从valgrind得到了25个错误。其中23个错误来自同一上下文。我没有失忆,但我不知道确切的问题出在哪里。我得到了我想要的确切输出,但仍然无法解决内存错误 我已经添加了valgrind输出和相关代码部分 Valgrind输出 HEAP SUMMARY: ==30142== in use at exit: 0 bytes in 0 blocks ==30142== total heap usage: 76 allocs, 77 frees, 2,146 bytes allocate

我从valgrind得到了25个错误。其中23个错误来自同一上下文。我没有失忆,但我不知道确切的问题出在哪里。我得到了我想要的确切输出,但仍然无法解决内存错误

我已经添加了valgrind输出和相关代码部分

Valgrind输出

HEAP SUMMARY:
==30142==     in use at exit: 0 bytes in 0 blocks
==30142==   total heap usage: 76 allocs, 77 frees, 2,146 bytes allocated
==30142== 
==30142== All heap blocks were freed -- no leaks are possible
==30142== 
==30142== ERROR SUMMARY: 25 errors from 3 contexts (suppressed: 0 from 0)
==30142== 
==30142== 1 errors in context 1 of 3:
==30142== Invalid free() / delete / delete[] / realloc()
==30142==    at 0x4C2B06D: free (vg_replace_malloc.c:540)
==30142==    by 0x400D79: main (pa2.c:201)
==30142==  Address 0x52063e0 is 0 bytes inside a block of size 24 free'd
==30142==    at 0x4C2B06D: free (vg_replace_malloc.c:540)
==30142==    by 0x4009D6: byte_to_char (pa2.c:75)
==30142==    by 0x400CBC: main (pa2.c:157)
==30142==  Block was alloc'd at
==30142==    at 0x4C2C089: calloc (vg_replace_malloc.c:762)
==30142==    by 0x400DE6: createNode (tree.c:10)
==30142==    by 0x4009B3: byte_to_char (pa2.c:73)
==30142==    by 0x400CBC: main (pa2.c:157)
==30142== 
==30142== 
==30142== 1 errors in context 2 of 3:
==30142== Invalid read of size 1
==30142==    at 0x400F0A: peek (stack.c:27)
==30142==    by 0x40083C: remove_zeroes (pa2.c:27)
==30142==    by 0x400CCE: main (pa2.c:165)
==30142==  Address 0x5206490 is 0 bytes inside a block of size 24 free'd
==30142==    at 0x4C2B06D: free (vg_replace_malloc.c:540)
==30142==    by 0x4008E5: byte_to_char (pa2.c:50)
==30142==    by 0x400CBC: main (pa2.c:157)
==30142==  Block was alloc'd at
==30142==    at 0x4C2C089: calloc (vg_replace_malloc.c:762)
==30142==    by 0x400DE6: createNode (tree.c:10)
==30142==    by 0x4008C2: byte_to_char (pa2.c:48)
==30142==    by 0x400CBC: main (pa2.c:157)
==30142== 
==30142== 
==30142== 23 errors in context 3 of 3:
==30142== Invalid read of size 1
==30142==    at 0x400D1F: main (pa2.c:175)
==30142==  Address 0x52063e0 is 0 bytes inside a block of size 24 free'd
==30142==    at 0x4C2B06D: free (vg_replace_malloc.c:540)
==30142==    by 0x4009D6: byte_to_char (pa2.c:75)
==30142==    by 0x400CBC: main (pa2.c:157)
==30142==  Block was alloc'd at
==30142==    at 0x4C2C089: calloc (vg_replace_malloc.c:762)
==30142==    by 0x400DE6: createNode (tree.c:10)
==30142==    by 0x4009B3: byte_to_char (pa2.c:73)
==30142==    by 0x400CBC: main (pa2.c:157)
==30142== 
==30142== ERROR SUMMARY: 25 errors from 3 contexts (suppressed: 0 from 0)
主函数调用(和之后)


所有三个错误上下文都是由于代码中的两个位置导致的,即在将节点放入堆栈后删除节点的位置:

for(int i=0; i < size; i++){
    if(topology[i] == 0){
        //char_top[i] = 48; //ASCII Value of 0
        node = createNode(48, 0);
        push(stack, node);
        free(node);                // the target of node is still referenced
        numOfBytes++;
    } else{
        ascii_val = 0xff;
        //char_top[i] = 49; //ASCII Value of 1
        node1 = createNode(49, 1);
        push(stack, node1);
        free(node1);                 // the target of node is still referenced
for(int i=0;i

您需要删除这两个释放的调用,而不是释放节点,直到堆栈上的引用弹出。

我理解您的意思,但在这种情况下,我开始丢失内存,因为我没有释放在这些节点中创建的上一个节点,我丢失了该节点。我不确定在调用push之后,如何在没有内存泄漏的情况下执行您解释的操作(堆栈,节点),节点在堆栈上被引用。稍后,将引用从堆栈移动到reverse_stack.tmp=pop(&reverse_stack);在上面显示的行之后,添加以下内容:free(tmp);
int byte_to_char(char *topology, Snode** stack, int size){
    int numOfBytes = 0;
    char ascii_concat[8]; 
    unsigned char ascii_val = 0;  
    int countChar =0;
    Tnode* node = NULL;
    Tnode* node1 = NULL;
    Tnode* node2 = NULL;
    //char char_top[80];
    for(int i=0; i < size; i++){
        if(topology[i] == 0){
            //char_top[i] = 48; //ASCII Value of 0
            node = createNode(48, 0);
            push(stack, node);
            free(node);
            numOfBytes++;
        } else{
            ascii_val = 0xff;
            //char_top[i] = 49; //ASCII Value of 1
            node1 = createNode(49, 1);
            push(stack, node1);
            free(node1);
            numOfBytes++;
            i++; //skip the leaf node indicator
            countChar++;  
            for(int j=0; j<8; j++){
                ascii_concat[j] = topology[i++];
            }
            i--;
            for(int j=0; j<8; j++){
                if(ascii_concat[j] == 0){
                    ascii_val = (1 << (j)) ^ ascii_val;
                }
            }
            //printf("%c\n", ascii_val);
            //Create leaf node
            //char_top[i] = ascii_val;

            //byte_to_char (pa2.c:73)
            node2 = createNode(ascii_val, 2);
            push(stack, node2);
            free(node2);

            numOfBytes = numOfBytes + 8;
            //count characters in header
        }
        countChar++;
    }

    // printf("chars\n");
    // for(int i=0;i < 24; i++){
    //    printf("%c", char_top[i]);
    // }
    // free(node);
    return numOfBytes;
    
}
Tnode* createNode(char id, int flag){
   Tnode* n = (Tnode*) calloc(1, sizeof(Tnode));
   //Tnode* n = (Tnode*) malloc(sizeof(Tnode));
   n->id = id;
   n->flag = flag;
   return n;
}
for(int i=0; i < size; i++){
    if(topology[i] == 0){
        //char_top[i] = 48; //ASCII Value of 0
        node = createNode(48, 0);
        push(stack, node);
        free(node);                // the target of node is still referenced
        numOfBytes++;
    } else{
        ascii_val = 0xff;
        //char_top[i] = 49; //ASCII Value of 1
        node1 = createNode(49, 1);
        push(stack, node1);
        free(node1);                 // the target of node is still referenced