| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4302 人关注过本帖
标题:为什么释放结构体内成员的内存会出错???
只看楼主 加入收藏
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
结帖率:90%
收藏
已结贴  问题点数:20 回复次数:19 
为什么释放结构体内成员的内存会出错???
这是代码,大家注意我给结构体内char word*分配了内存,理论上释放应该先释放它,然后再释放结构体,但是,运行结果老是有问题?我觉得我的释放顺序没有问题
程序代码:
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

#define word_len 100
#define str_len 1000

typedef struct Word Word;

struct Word
{
    char* word;
    int count;
    Word* next;
};

char* get_text();
Word* get_memory();
void get_word(char* pstr);
void add_word(char* word);

Word* first = NULL;

int main(void)
{
    char* str = NULL;
    str = get_text();
    get_word(str);
    Word* temp = NULL;
    temp = first;
    int count = 0;
    while (temp)
    {
        printf_s("%-10s %-10d  ", temp->word, temp->count);
        count++;
        if (count % 2 == 0)
        {
            printf("\n");
        }
        temp = temp->next;
    }
    temp = first;
    while (temp)
    {
        first = temp;
        temp = temp->next;
        free(first);
    }
    free(str);
    return 0;
}

char* get_text()
{
    char word[word_len] = { 0 };
    char* pstr = malloc(str_len*sizeof(char));
    char* temp = NULL;
    pstr[0] = '\0';
    printf_s("Please enter text! If you want to finish text input.(press twice 'enter' key).\n");
    while (true)
    {
        fgets(word, word_len, stdin);
        if (word[0] == '\n')
        {
            break;
        }
        if (str_len - strlen(pstr) <= strlen(word))
        {
            temp = realloc(pstr, (str_len + strlen(word))*sizeof(char));
            pstr = temp;
            temp = NULL;
        }
        strcat_s(pstr, str_len, word);
    }
    return pstr;
}

Word* get_memory(char* words)
{
    Word* pcount = (Word*)malloc(sizeof(Word));
    pcount->word = (char*)malloc(strlen(words) + 1);
    strcpy_s(pcount->word, strlen(pcount->word),words);
    pcount->count = 1;
    pcount->next = NULL;
    return pcount;
}

void get_word(char* pstr)
{
    char* pwords = NULL;
    char* ptr = NULL;
    char delimiters[] = " \n\".,:;!?)(";
    pwords = strtok_s(pstr, delimiters, &ptr);
    if (!pwords)
    {
        printf_s("No words found. Ending program.\n");
        return;
    }

    while (pwords)
    {
        add_word(pwords);
        pwords = strtok_s(NULL, delimiters, &ptr);
    }
}

void add_word(char* words)
{
    Word* pcurrent = NULL;
    Word* temp = NULL;
    if (!first)
    {
        first = get_memory(words);
        return;
    }
    pcurrent = first;
    while (pcurrent)
    {
        if (strcmp(pcurrent->word, words) == 0)
        {
            ++pcurrent->count;
            return;
        }
        temp = pcurrent;
        pcurrent = pcurrent->next;
    }
    temp->next = get_memory(words);
}
搜索更多相关主题的帖子: include 结构体 
2016-04-13 20:51
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
收藏
得分:0 
如果不释放char* word,编译后正确运行
2016-04-13 20:52
alice_usnet
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:18
帖 子:370
专家分:2020
注 册:2016-3-7
收藏
得分:0 
int main()
{
    ...
    /*free(str);  str不是动态分配的,不能free*/
    return 0;
}

未佩好剑,转身便已是江湖
2016-04-13 20:59
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
收藏
得分:0 
回复 3楼 alice_usnet
是动态内存。在get_text函数中
2016-04-13 21:02
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
收藏
得分:0 
没在你的代码里面看到 你释放 word 的操作

https://zh.
2016-04-13 21:07
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
收藏
得分:0 
回复 5楼 lin5161678
不好意思粘错了
程序代码:
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

#define word_len 100
#define str_len 1000

typedef struct Word Word;

struct Word
{
    char* word;
    int count;
    Word* next;
};

char* get_text();
Word* get_memory();
void get_word(char* pstr);
void add_word(char* word);

Word* first = NULL;

int main(void)
{
    char* str = NULL;
    str = get_text();
    get_word(str);
    Word* temp = NULL;
    temp = first;
    int count = 0;
    while (temp)
    {
        printf_s("%-10s %-10d  ", temp->word, temp->count);
        count++;
        if (count % 2 == 0)
        {
            printf("\n");
        }
        temp = temp->next;
    }
    temp = first;
    while (temp)
    {
        first = temp;
        temp = temp->next;
        free(first);
    }
    free(str);
    return 0;
}

char* get_text()
{
    char word[word_len] = { 0 };
    char* pstr = malloc(str_len*sizeof(char));
    char* temp = NULL;
    pstr[0] = '\0';
    printf_s("Please enter text! If you want to finish text input.(press twice 'enter' key).\n");
    while (true)
    {
        fgets(word, word_len, stdin);
        if (word[0] == '\n')
        {
            break;
        }
        if (str_len - strlen(pstr) <= strlen(word))
        {
            temp = realloc(pstr, (str_len + strlen(word))*sizeof(char));
            pstr = temp;
            temp = NULL;
        }
        strcat_s(pstr, str_len, word);
    }
    return pstr;
}

Word* get_memory(char* words)
{
    Word* pcount = (Word*)malloc(sizeof(Word));
    pcount->word = (char*)malloc(strlen(words) + 1);
    strcpy_s(pcount->word, strlen(pcount->word),words);
    pcount->count = 1;
    pcount->next = NULL;
    return pcount;
}

void get_word(char* pstr)
{
    char* pwords = NULL;
    char* ptr = NULL;
    char delimiters[] = " \n\".,:;!?)(";
    pwords = strtok_s(pstr, delimiters, &ptr);
    if (!pwords)
    {
        printf_s("No words found. Ending program.\n");
        return;
    }

    while (pwords)
    {
        add_word(pwords);
        pwords = strtok_s(NULL, delimiters, &ptr);
    }
}

void add_word(char* words)
{
    Word* pcurrent = NULL;
    Word* temp = NULL;
    if (!first)
    {
        first = get_memory(words);
        return;
    }
    pcurrent = first;
    while (pcurrent)
    {
        if (strcmp(pcurrent->word, words) == 0)
        {
            ++pcurrent->count;
            return;
        }
        temp = pcurrent;
        pcurrent = pcurrent->next;
    }
    temp->next = get_memory(words);
}
2016-04-13 21:28
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
收藏
得分:0 
回复 6楼 foxeer
少年
如果我没猜错的话
你再一次 贴错代码
这代码还是没有 free word 的操作啊

建议你用编辑功能修改代码 不要再赋值粘贴了

https://zh.
2016-04-13 21:52
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
收藏
得分:0 
回复 7楼 lin5161678
程序代码:
while (temp)
    {
        free(temp->word);
        first = temp;
        temp = temp->next;
        free(first);
    }
    free(str);
    return 0;
}
2016-04-14 09:01
alice_usnet
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:18
帖 子:370
专家分:2020
注 册:2016-3-7
收藏
得分:0 
建议你自己调试吧,自己找出错误总比别人指出错误在哪里好的多。
PS:从源代码中找出错误比在调试中发现错误要好,在调试中发现错误比在测试程序时发现错误要好,在测试程序中发现bug总比客户报告bug好;bug发现得越早修复bug所需的代价也就越小,你不能总指望客户给你报告bug。

未佩好剑,转身便已是江湖
2016-04-14 09:25
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
收藏
得分:0 
回复 9楼 alice_usnet
这个要是我能调试出来的话,我就不在这提问了
2016-04-14 09:28
快速回复:为什么释放结构体内成员的内存会出错???
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.068375 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved