动态记忆问题(我认为) [英] dynamic memory problem(i think)

查看:74
本文介绍了动态记忆问题(我认为)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


i有一个二元树节点的结构


typedef struct treenode

{

char * word;

struct treenode * right;

struct treenode * left;

} TreeNode;


和元素*字,我为它动态分配内存如下

(也适用于TreeNode)

addNode(TreeNode *,char *);


char * token,buf [BUF_MAX],* memWord;

TreeNode * root;


/ *做内存分配检查* /


while((fgets(buf,BUF_MAX,filePtr))!= NULL)

{

/ *这里做缓冲处理* /

token = strtok(buf," \ n");

while(令牌!= NULL)

{

/ *以下是对吗? * /

memWord = malloc(sizeof(char)* strlen(token);

strcpy(memWord,token);


addNode(root,memWord);

token = strtok(NULL," \ n");

}

}

上面的代码从文件读取一行,然后将行标记为

读入令牌,然后分配与

令牌相同长度的内存,然后将令牌复制到memWord并传递给函数

将它添加到root指向的BST。


我的问题和问题是,当我从列表中删除一个节点

并为节点内的单词和节点释放内存

本身,我没有内存泄漏或任何内容(因为根据

valgrind我有更多的免费'然后分配),然后将它们打印出来

(按顺序),好吧,我得到奇怪的字符和mumble jumble for insted

。但如果我不在节点内释放这个词(并得到一些

内存泄漏)然后打印出它实际上工作,即删除

的节点消失了,没有奇怪的字符和所有。所以无论如何

这里是我用来删除节点的代码(递归)


delete(char * w,TreeNode T)

{

TreeNode tmpNode;

if(T == NULL)

返回T;

如果(strcmp(w,T-> word)< 0)

T-> left = delete(w,T-> left);

else

if(strcmp(w,T-> word)> 0)

T-> right = delete(w,T-> right);

else

{

if(T-> left&& T-> right)

{

tmpNode = findMin(T->右);

T-> word = tmpNode - >单词;

T-> right =删除(w,T->右);

}

其他

{

tmpNode = T;

if(T-> left == NULL)

T = T-> right ;

否则if(T-> right == NULL)

T = T-> left;


免费(tmpNode-> word);

免费(tmpNode);

}


}

返回T;

}

Hi all,

i have this struct for a binary tree node

typedef struct treenode
{
char *word;
struct treenode *right;
struct treenode *left;
}TreeNode;

and to the element *word, i dynamically allocate memory for it as below
(and also for the TreeNode too)
addNode(TreeNode*,char*);

char *token, buf[BUF_MAX], *memWord;
TreeNode *root;

/*do memory allocation check */

while((fgets(buf,BUF_MAX,filePtr)) != NULL)
{
/* do buffer handling here */
token = strtok(buf,"\n");
while (token != NULL)
{
/* is the following right ? */
memWord = malloc(sizeof(char) * strlen(token);
strcpy(memWord,token);

addNode(root,memWord);
token = strtok(NULL, "\n");
}
}
the code above reads a line from a file and then tokenizes the line
read into tokens and then allocates memory in the same length of the
token, then copies the token into memWord and passes to a function to
add it to the BST pointed by root.

My question and problem is that , when i delete a node from the list
and free the memory for both the word inside the node and the node
itself, i get no memory leaks or whatsoever (because according to
valgrind i have more free''s then allocations) and then print them out
(in-order), well, i get weird characters and mumble jumble for insted
of words. But if i dont free the word inside the node (and get some
memory leaks) then print them out it actually works, i.e the nodes that
deleted are gone and there is no weird characters and all. So anyway
here is the code that i use to delete a node (recursive)

delete(char *w, TreeNode T)
{
TreeNode tmpNode;
if (T == NULL)
return T;
if (strcmp(w, T->word) < 0 )
T->left = delete(w,T->left);
else
if (strcmp(w, T->word) > 0)
T->right = delete(w,T->right);
else
{
if(T->left && T->right)
{
tmpNode = findMin(T->right);
T->word = tmpNode -> word;
T->right = delete(w,T->right);
}
else
{
tmpNode = T;
if (T->left == NULL)
T= T->right;
else if (T->right == NULL)
T= T->left;

free(tmpNode->word);
free(tmpNode);
}

}

return T;
}

推荐答案

placid写道:
大家好,

我有一个二元树节点的结构

typedef struct treenode
{
char * word;
struct treenode * right;
struct treenode * left;
} TreeNode;

和元素* word,我为它动态分配内存,如下所示
(而且对于TreeNode也是如此)

请发布* real *代码。复制并粘贴!
addNode(TreeNode *,char *);

char * token,buf [BUF_MAX],* memWord;
TreeNode * root;

/ *做内存分配检查* /

while((fgets(buf,BUF_MAX,filePtr))!= NULL)
{
/ *在这里做缓冲处理* /
token = strtok(buf," \ n");
while(令牌!= NULL)
{
/ *以下是对吗? * /
memWord = malloc(sizeof(char)* strlen(令牌);


这是你的问题。

首先,sizeof(char)== 1根据定义,所以它是多余的。

但更重要的是,你已经分配了一个char太少了;你

没有为终止字符串的null字符留出空间。


制作以上行:

memWord = malloc(strlen(token) + 1));


[顺便说一句 - 我知道你没有发布*真正的*代码,因为你错过了收盘

以上。]


你还需要检查malloc()是否没有返回NULL。

strcpy(memWord,token);
Hi all,

i have this struct for a binary tree node

typedef struct treenode
{
char *word;
struct treenode *right;
struct treenode *left;
}TreeNode;

and to the element *word, i dynamically allocate memory for it as below
(and also for the TreeNode too)
Please post *real* code. Copy and paste!
addNode(TreeNode*,char*);

char *token, buf[BUF_MAX], *memWord;
TreeNode *root;

/*do memory allocation check */

while((fgets(buf,BUF_MAX,filePtr)) != NULL)
{
/* do buffer handling here */
token = strtok(buf,"\n");
while (token != NULL)
{
/* is the following right ? */
memWord = malloc(sizeof(char) * strlen(token);
Here''s your problem.
First of all, sizeof(char) == 1 by definition, so it''s superfluous.
More important, however, is that you''ve allocated one char too few; you
haven''t left room for the null char that terminates the string.

Make the above line:
memWord = malloc(strlen(token) + 1));

[BTW - I know you haven''t posted *real* code as you''re missing a closing
paren above.]

You also need to check that malloc() did not return NULL.
strcpy(memWord,token);




现在,由于你没有分配足够的内存,你对strcpy()

的调用会调用未定义的行为。你很幸运,鼻子恶魔(tm)确实没有接下来!


[snip]


HTH,

- g

-

Artie Gold - 德克萨斯州奥斯汀
http://goldsays.blogspot.com (新帖子8/5)
http://www.cafepress.com/goldsays

如果你没有什么可隐瞒的,你没有尝试!



Now, since you haven''t allocated enough memory, your call to strcpy()
invokes undefined behavior. You were fortunate that nasal demons(tm) did
not ensue!

[snip]

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you''re not trying!"




Artie Gold写道:

Artie Gold wrote:
placid写道:
大家好,

我有一个二元树节点的结构

typedef struct treenode
{
char * word; <结构treenode * right;
struct treenode * left;
} TreeNode;

和元素* word,我为它动态分配内存,如下所示
(也适用于TreeNode)
请发布*真实*代码。复制并粘贴!
Hi all,

i have this struct for a binary tree node

typedef struct treenode
{
char *word;
struct treenode *right;
struct treenode *left;
}TreeNode;

and to the element *word, i dynamically allocate memory for it as below
(and also for the TreeNode too)
Please post *real* code. Copy and paste!




是的,因为某些原因,我总是有两个代码副本(非常糟糕

想法)一个在我的笔记本电脑上和一个UNIX服务器,所以我不得不再次输入

所以我只是想节省一些时间。对不起男人



yeah, see for some reason i always have two copies of code(really bad
idea) one on my laptop and one a UNIX server, so i had to type it out
all again so i was just trying to save some time. Sorry man


addNode(TreeNode *,char *);

char * token,buf [BUF_MAX ],* memWord;
TreeNode * root;

/ *做内存分配检查* /

while((fgets(buf,BUF_MAX,filePtr))! = NULL)
{
/ *在这里做缓冲区处理* /
token = strtok(buf," \ n");
while(token!= NULL)
{
/ *是以下的权利? * /
memWord = malloc(sizeof(char)* strlen(token);

addNode(TreeNode*,char*);

char *token, buf[BUF_MAX], *memWord;
TreeNode *root;

/*do memory allocation check */

while((fgets(buf,BUF_MAX,filePtr)) != NULL)
{
/* do buffer handling here */
token = strtok(buf,"\n");
while (token != NULL)
{
/* is the following right ? */
memWord = malloc(sizeof(char) * strlen(token);



这是你的问题。
首先,sizeof(char )== 1根据定义,所以它是多余的。
更重要的是,你已经分配了一个字符太少;你没有为空字符留下空间这将终止字符串。



Here''s your problem.
First of all, sizeof(char) == 1 by definition, so it''s superfluous.
More important, however, is that you''ve allocated one char too few; you
haven''t left room for the null char that terminates the string.




所以strlen不包括长度计数中的空字符

制作以上行:
memWord = malloc(strlen(token)+ 1));

[顺便说一句 - 我知道你没有发布*真正的*代码因为你错过了闭幕
paren上面。]

你还需要检查malloc()没有返回NULL。


代码可读性牺牲了!



so then strlen does not include the null character in the lenght count

Make the above line:
memWord = malloc(strlen(token) + 1));

[BTW - I know you haven''t posted *real* code as you''re missing a closing
paren above.]

You also need to check that malloc() did not return NULL.
sacrificed for code readablity !

strcpy(memWord,token);
现在,因为你没有足够的分配内存,你对strcpy()的调用
调用未定义的行为。你很幸运,鼻子恶魔(tm)没有随之而来!
strcpy(memWord,token);
Now, since you haven''t allocated enough memory, your call to strcpy()
invokes undefined behavior. You were fortunate that nasal demons(tm) did
not ensue!




这就是为什么我会得到奇怪的角色和咕for的混蛋

[snip]

HTH,
- g

你是一个救命的男人


非常感谢

-
Artie Gold - 德克萨斯州奥斯汀
http://goldsays.blogspot.com (新帖子8/5)
http://www.cafepress.com/goldsays
如果你没有什么可隐瞒的,你就不会尝试!



thats why i get "weird characters and mumble jumble"

[snip]

HTH,
--ag
your a life saver man

greatly appreciated

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you''re not trying!"






Artie Gold写道:
Artie Gold wrote:
placid写道:
placid wrote:
大家好,
我有一个二元树节点的结构

typedef struct treenode
{* * char * word;
struct treenode * right;
struct treenode *离开;
} TreeN ode;

和元素*字,我为它动态分配内存如下
(也适用于TreeNode)
Hi all,

i have this struct for a binary tree node

typedef struct treenode
{
char *word;
struct treenode *right;
struct treenode *left;
}TreeNode;

and to the element *word, i dynamically allocate memory for it as below
(and also for the TreeNode too)


请发布* real *码。复制并粘贴!


Please post *real* code. Copy and paste!


addNode(TreeNode *,char *);

char * token,buf [BUF_MAX],* memWord;
TreeNode * root;

/ *做内存分配检查* /

while((fgets(buf,BUF_MAX,filePtr))!= NULL)
{
/ *在这里做缓冲区处理* /
token = strtok(buf," \ n");
while(token!= NULL)
{
/ *是以下吗? * /
memWord = malloc(sizeof(char)* strlen(token);

addNode(TreeNode*,char*);

char *token, buf[BUF_MAX], *memWord;
TreeNode *root;

/*do memory allocation check */

while((fgets(buf,BUF_MAX,filePtr)) != NULL)
{
/* do buffer handling here */
token = strtok(buf,"\n");
while (token != NULL)
{
/* is the following right ? */
memWord = malloc(sizeof(char) * strlen(token);



这是你的问题。
首先,sizeof(char )== 1根据定义,所以它是多余的。


Here''s your problem.
First of all, sizeof(char) == 1 by definition, so it''s superfluous.




然而,使用


memWord = malloc(( sizeof * memWord)*(strlen(令牌)+1));


是一个更好的练习...

-

一个人的自由停止在其他人开始的地方

Giannis Papadopoulos
http://dop.users.uth.gr/

塞萨利大学

计算机与通信工程部门



However, using

memWord = malloc( (sizeof *memWord) * (strlen(token)+1) );

is a much better practice...
--
one''s freedom stops where others'' begin

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.


这篇关于动态记忆问题(我认为)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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