Memory leak somewhere in the code

2019-09-07 02:45发布

This code works fine with a txt containing 1000 words, but when I use 10000 words, it stops responding.

also, the main.c works with 10000 words when I use dynamic array instead of binary tree. so I think the problem is somewhere in the tree.c code...

tree.h

#ifndef TREE_H_
#define TREE_H_

typedef struct Item{
    char* key;
    int no;
} TItem;

typedef struct No{
    TItem item;
    struct No* pLeft;
    struct No* pRight;
} TNo;

void TTree_Insert (TNo**, char[]);
void TTree_Print (TNo*);

#endif

tree.c

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

TNo* TNo_Create (char* c){
    TNo* pNo = malloc(sizeof(TNo));
    pNo->item.key = malloc(sizeof(char)*strlen(c));
    strcpy(pNo->item.key, c);
    pNo->item.no = 1;
    pNo->pLeft = NULL;
    pNo->pRight = NULL;
    return pNo;
}

void TTree_Insert (TNo** pRoot, char word[80]){
    char* c = malloc(sizeof(char)*strlen(word));
    strcpy(c, word);
    TNo** pAux;
    pAux = pRoot;
    while (*pAux != NULL){
        if (strcmp(c, (*pAux)->item.key) < 0) pAux = &((*pAux)->pLeft);
        else if (strcmp(c, (*pAux)->item.key) > 0) pAux = &((*pAux)->pRight);
        else{
            (*pAux)->item.no++;
            return;
        }
    }
    *pAux = TNo_Create(c);
    return;
}

void TTree_Print (TNo *p){
    if (p == NULL) return;
    TTree_Print (p->pLeft);
    printf("%s - %d", p->item.key, p->item.no);
    TTree_Print (p->pRight);
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "tree.h"

int main(){
    TNo* pRoot = NULL;
    FILE* txt = fopen("Loremipsum.txt", "r");
    char aux[80];
    int c, x = 0;

    while ((c = fgetc(txt)) != EOF){
        while (!(isalpha((char)c))) c = fgetc(txt);
        while (isalpha((char)c)) {
            if (isupper((char)c)) c = c+32;
            if (islower((char)c)) aux[x++] = (char)c;
            c = fgetc(txt);
        }
        aux[x] = '\0';
        TTree_Insert(&pRoot, aux);
        x = 0;
        aux[0] = '\0';
    }
    TTree_Print(pRoot);
    fclose(txt);
    return 0;
}

1条回答
别忘想泡老子
2楼-- · 2019-09-07 03:27

Apart from your typo (you forgot to add 1 in malloc(sizeof(char)*strlen(word));) there are memory leaks in your program.

You already allocated memory pointed to by pointer c. So in function TNo_Create you need not to allocate memory anew.

Also there is a memory leak in function TTree_Print in case when a node with given key was found.

The functions can look the following way

static TNo* TNo_Create( char* c )
{
    TNo* pNo = malloc( sizeof( TNo ) );

    pNo->item.key = c;

    pNo->item.no = 1;
    pNo->pLeft = NULL;
    pNo->pRight = NULL;

    return pNo;
}

void TTree_Insert ( TNo** pRoot, const char word[80] )
{
    TNo** pAux = pRoot;

    while ( *pAux != NULL )
    {
        if ( strcmp( word, (*pAux)->item.key) < 0) pAux = &(*pAux)->pLeft;
        else if (strcmp( word, (*pAux)->item.key) > 0) pAux = &(*pAux)->pRight;
        else{
            (*pAux)->item.no++;
            return;
        }
    }

    char* c = malloc( strlen( word ) + 1 );
    strcpy(c, word);

    *pAux = TNo_Create(c);
}

Also you could check whether malloc was successful.

查看更多
登录 后发表回答