| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4270 人关注过本帖
标题:为什么释放结构体内成员的内存会出错???
取消只看楼主 加入收藏
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
结帖率:90%
收藏
已结贴  问题点数:20 回复次数:10 
为什么释放结构体内成员的内存会出错???
这是代码,大家注意我给结构体内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
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
收藏
得分:0 
回复 3楼 alice_usnet
是动态内存。在get_text函数中
2016-04-13 21:02
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
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
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
收藏
得分:0 
回复 9楼 alice_usnet
这个要是我能调试出来的话,我就不在这提问了
2016-04-14 09:28
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
收藏
得分:0 
回复 11楼 grmmylbs
这个试试,前面的我粘错了,这个肯定出错,运行能产生结果,但是系统显示程序崩溃,肯定是内存释放出了问题
程序代码:
#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)
    {
        free(temp->word);
        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-14 09:57
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册
2016-04-14 10:01
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
收藏
得分:0 
回复 14楼 grmmylbs
能说说为啥吗?我感觉咱们两个使用的命令不一样,但是目的是一样的,为什么strncpy就可以呢????
2016-04-14 11:16
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
收藏
得分:0 
回复 16楼 lin5161678
还想请教个问题,我终于知道我错的地方了
    pcount->word = (char*)malloc(strlen(words) + 1);
    size_t pcount_word = sizeof(pcount->word);
    size_t words_1 = strlen(words) + 1;

理论上pcount_word和words_1结果应该是相等的,但是,我得到的结果却不相等。

请把刚才的程序,分配内存部分该为一下的结果,你看看,pcount_word结果固定不变,然而words是改变的,这到底为啥?
程序代码:
Word* get_memory(char* words)
{
    size_t pcount_word = 0;
    size_t words_1 = 0;
    Word* pcount = (Word*)malloc(sizeof(Word));
    pcount->word = (char*)malloc(strlen(words) + 1);
    pcount_word = sizeof(pcount->word);
    words_1 = strlen(words) + 1;
    printf("pcount_word:%d  words_1:%d\n", pcount_word, words_1);
    strncpy_s(pcount->word, strlen(words)+1,words, strlen(words));
    pcount->count = 1;
    pcount->next = NULL;
    return pcount;
}
2016-04-14 13:20
快速回复:为什么释放结构体内成员的内存会出错???
数据加载中...
 
   



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

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