Memory leak in basic stack

2019-08-30 02:40发布

I'm working on a real simple stack implementation, and I can't seem to figure out why I have a memory leak. What I expect from the code is that 5 nodes are allocated in push() and 5 nodes are freed in displayAndDestroy(). But Valgrind says I've allocated 6 nodes worth of data and only freed 5. I've been staring at this for a while and I'm not exactly sure where I went wrong.


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

typedef struct STACK{
    char data[100];
    struct STACK *next;
} stack;

stack *push( stack *oldTop, char *data )
{
    stack *newTop = malloc(sizeof(stack));
    newTop->next = oldTop;

    if(!data){
        strcpy(newTop->data, newTop->next->data);
    } else{
        strcpy(newTop->data, data);
    }

    return( newTop );
}

void displayAndDestroy( stack *top )
{
    stack *currentTop = top;
    stack *temp;

    int i=0;
    while(currentTop){

        printf("stack%d: %s\n", i, currentTop->data );

        temp = currentTop->next;
        free(currentTop);
        currentTop = temp;

        i++;
    }
}

stack *initializer( stack *top, char *fileName )
{
    char word[100];
    char ch;

    FILE *fr = fopen(fileName, "r");

    int i=0;
    while( (ch=fgetc(fr)) != EOF ){
        if( ch == '>' ){
            fscanf(fr, "%s\n", word);
            top = push( top, word );
            i++;
        }
    }

    return top;
}

int main()
{
    stack *top = NULL;

    top = initializer( top, "testData.txt" );

    displayAndDestroy( top );

    return 0;
}

testData.txt

garbage

>stringone
>2nd string
>s3

moregarbage
>THE 4FOURTH4 STRING

>5
finalgarbage

Valgrind says:

==19446== Memcheck, a memory error detector
==19446== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==19446== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==19446== Command: ./test
==19446== 
stack0: 5
stack1: THE
stack2: s3
stack3: 2nd
stack4: stringone
==19446== 
==19446== HEAP SUMMARY:
==19446==     in use at exit: 568 bytes in 1 blocks
==19446==   total heap usage: 6 allocs, 5 frees, 1,128 bytes allocated
==19446== 
==19446== 568 bytes in 1 blocks are still reachable in loss record 1 of 1
==19446==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19446==    by 0x4EA544C: __fopen_internal (iofopen.c:73)
==19446==    by 0x400880: initializer (test.c:47)
==19446==    by 0x400921: main (test.c:65)
==19446== 
==19446== LEAK SUMMARY:
==19446==    definitely lost: 0 bytes in 0 blocks
==19446==    indirectly lost: 0 bytes in 0 blocks
==19446==      possibly lost: 0 bytes in 0 blocks
==19446==    still reachable: 568 bytes in 1 blocks
==19446==         suppressed: 0 bytes in 0 blocks
==19446== 
==19446== For counts of detected and suppressed errors, rerun with: -v
==19446== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

标签: c stack
1条回答
地球回转人心会变
2楼-- · 2019-08-30 03:17

Your memory leak is from the fopen call, as you can see from the stack trace of the leaking allocation -- it comes from the fact that you call fopen to open a file (which allocates a buffer struct on the heap to manage the file) and then never call fclose.

Add a call to fclose after you're done reading the file, and it will go away.

查看更多
登录 后发表回答