【问题标题】:Memory leak somewhere in the code代码中某处的内存泄漏
【发布时间】:2014-11-16 20:38:48
【问题描述】:

此代码适用于包含 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);
}

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

【问题讨论】:

  • 另外,我如何识别这里的所有代码而不必在每一行中使用四个空格?为什么我不能只使用
     标签?
  • 长度为 4 的字符串需要 5 个字节来存储。 malloc(strlen(c)) 减一,因为您无法存储终止的空字节。
  • 您需要为您的密钥多分配一个 strlen
  • 哦,我很惊讶这是问题所在,因为在动态数组中我没有在 malloc 中使用 +1 并且它起作用了......
  • @ColdLucas - 大多数 IDE 可以将制表符转换为空格并为您缩进

标签: c memory-leaks


【解决方案1】:

除了您的错字(您忘记在 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 是否成功。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多