代码中的某处内存泄漏 [英] Memory leak somewhere in the code

查看:83
本文介绍了代码中的某处内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码对于包含1000个单词的txt可以正常工作,但是当我使用10000个单词时,它将停止响应.

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

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

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

推荐答案

除了拼写错误(您忘记在malloc(sizeof(char)* strlen(word))中添加1),程序中还存在内存泄漏.

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

您已经分配了指针c指向的内存.因此,在功能TNo_Create中,您无需重新分配内存.

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

如果找到具有给定密钥的节点,功能TTree_Print中也会发生内存泄漏.

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

您还可以检查malloc是否成功.

Also you could check whether malloc was successful.

这篇关于代码中的某处内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆