c - 代码某处内存泄漏

标签 c memory-leaks

此代码适用于包含 1000 个单词的 txt,但当我使用 10000 个单词时,它会停止响应。

另外,当我使用动态数组而不是二叉树时,main.c 可以处理 10000 个单词。所以我认为问题出在 tree.c 代码的某处......

树.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

树.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);
}

主.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;
}

最佳答案

除了你的拼写错误(你忘记在 malloc(sizeof(char)*strlen(word)); 中添加 1)之外,你的程序中还有内存泄漏。

您已经分配了指针 c 指向的内存。所以在函数 TNo_Create 中你不需要重新分配内存。

在找到具有给定键的节点时,函数 TTree_Print 中也存在内存泄漏。

函数如下所示

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);
}

也可以查看malloc是否成功

关于c - 代码某处内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26961797/

相关文章:

c - 直接插入排序的错误 C 实现

使用 for 循环从缓冲区复制到缓冲区,适用于除 0 之外的所有情况,难倒

javascript - 优化Javascript函数内存泄漏

c++ - 按值填充 std::vector 时,是否会删除动态分配的对象指针?

c++ - cuda统一内存泄漏

c++ - 程序执行时重新编译代码

c - 在 C 中替换宏中的宏

c - 参数类型不完整

C# WinForm 内存泄漏

c - 如何释放从另一个函数返回的 char*?