gpt4 book ai didi

c - 修复 K&R 书中第 6.5 章示例中的内存泄漏(自引用结构)

转载 作者:行者123 更新时间:2023-12-02 21:57:45 25 4
gpt4 key购买 nike

我在 Visual Studio 中使用视觉泄漏检测器,当我运行程序时它会检测到一些泄漏。基本上,程序中有两个泄漏,一个是在 malloc 结构 *p 上检测到的,另一个是在其成员 (p->word) 中检测到的。我读了所有关于在 malloc 结构中释放内存的讨论。问题是我似乎找不到可以释放内存的地方,特别是当这是一个递归结构时。

这是代码

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

#include <stdlib.h>
#include <vld.h>

#define MAXWORD 100

struct tnode { /*the tree node */
char *word; /* points to itself */
int count; /* number of occurences */
struct tnode *left; /* left child */
struct tnode *right; /* right child */
};


struct tnode *addtree (struct tnode *, char *);
struct tnode *talloc(void);
void treeprint (struct tnode *);
char *strdupli (char *s);
int getword (char *, int);
void strcopy (char *, const char *);

int main(void)
{
struct tnode *root;
char word[MAXWORD];

root = NULL;
while(getword(word, MAXWORD) != EOF)
if( isalpha (word[0]))
root = addtree(root, word);
treeprint(root);
return 0;
}



int getword(char *word, int lim)
{
int c, getch(void);
void ungetch(int);
char *w = word;

while(isspace(c = getch()))
;
if(c != EOF)
*w++ = c;
if(!isalpha(c)) {
*w = '\0';
return c;
}
for( ; --lim > 0; w++)
if(!isalnum(*w = getch())) {
ungetch(*w);
break;
}
*w = '\0';
return word[0];

}

struct tnode *addtree (struct tnode *p, char *w)
{
int cond;

if (p == NULL) { /* a new word has arrived*/
p = talloc(); /* make a new node */
p->word = strdupli(w);
p->count = 1;
p->left = p->right = NULL;
} else if ((cond=strcmp(w, p->word))==0)
p->count++;
else if (cond < 0)
{
p->left = addtree (p->left, w);
} else
{
p->right = addtree(p->right, w);
}
return p;
}

void treeprint (struct tnode *p)
{
if(p != NULL) {
treeprint(p->left);
printf("%4d %s\n", p->count, p->word);
treeprint(p->right);
}
}


/* talloc: make a tnode */
struct tnode *talloc(void)
{
return (struct tnode *) malloc(sizeof(struct tnode));
}

char *strdupli (char *s) /* make a duplicate of s */
{
char *p;
p = (char *) malloc (strlen(s) + 1);
if (p != NULL)
strcopy(p, s);
return p;
}

void strcopy (char *s, const char *t)
{
while ((*s++ = *t++) != '\0')
;
}

#define BUFSIZE 100
char buf[BUFSIZE]; /* buffer for ungetch */
int bufp = 0; /* next free position in buf */
int getch(void) /* get a (possibly pushed-back) character */
{
return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c) /* push character back on input */
{
if (bufp >= BUFSIZE)
printf("ungetch: too many characters\n");
else
buf[bufp++] = c;
}

你需要看看 *addtree 函数

最佳答案

要解决这个问题,请创建一个类似于打印函数的递归函数来释放树。

一个例子:

void free_tree (struct tnode *p)
{
if(p != NULL) {
free_tree(p->left);
free_tree(p->right);
free( p->word );
free( p );
}
}

你可以把它放在treeprint之后的main中:

int main(void)
{
...
treeprint(root);
free_tree( root );
// ^^^^^^^^^^^^^^^
return 0;
}

关于c - 修复 K&R 书中第 6.5 章示例中的内存泄漏(自引用结构),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17462957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com