| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4223 人关注过本帖
标题:为什么释放结构体内成员的内存会出错???
只看楼主 加入收藏
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:0 
你是在什么条件下出错?
我执行了一下,输入了2个字符串,执行完没有发现异常。
2016-04-14 09:31
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
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:0 
不清楚strcpy_s的运行机制,改成下面的就可以了:
用strcpy_s似乎拷贝越界了,怎么改都不行

Word* get_memory(char* words)
{
    Word* pcount = (Word*)malloc(sizeof(Word));
    pcount->word = (char*)malloc(strlen(words) + 1);
    strncpy(pcount->word, words, strlen(words)+1);
    pcount->count = 1;
    pcount->next = NULL;
    return pcount;
}
2016-04-14 10:55
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
收藏
得分:0 
回复 14楼 grmmylbs
能说说为啥吗?我感觉咱们两个使用的命令不一样,但是目的是一样的,为什么strncpy就可以呢????
2016-04-14 11:16
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
收藏
得分:0 
程序代码:
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->word 不是字符串 不能作为strlen的参数
改成这样就没事了
    strcpy_s(pcount->word, strlen(words)+1, words);

https://zh.
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
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
收藏
得分:20 
回复 17楼 foxeer
sizeof 得到的是数据类型的大小
你的结构体成员 word 是一个指针类型

指针大小 和 指针存储的内容没关系

https://zh.
2016-04-14 13:27
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
收藏
得分:0 
strlen
计算的是字符串长度
计算方法是 扫描整个字符串直到找到 '\0'

是由字符串内容决定返回值大小的

https://zh.
2016-04-14 13:28
foxeer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:107
专家分:136
注 册:2015-12-29
收藏
得分:0 
回复 19楼 lin5161678
thank you
2016-04-14 14:59
快速回复:为什么释放结构体内成员的内存会出错???
数据加载中...
 
   



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

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