在Calloc创建的结构数组上使用Free()时,程序会崩溃

发布于 2025-02-07 04:42:42 字数 1159 浏览 2 评论 0原文

我有此功能,它使用calloc来创建结构命题的数组。每当我尝试免费另一个fuction的结果数组时,它就会崩溃。

proposition* get_matching_propositions(char *pcde, proposition *propositions, int *match_count)
{    
    proposition *matches;
    int         count       = get_count_from_stream(ptf_proposition, sizeof(proposition)),
                cptr_match  = 0;

    for (int i = 0; i < count; i++)
    {
        if (strcmp(propositions[i].cde, pcde) == NULL)
        {
            cptr_match++;
        }
    }

    matches = (proposition*) calloc (cptr_match, sizeof(propositions));
    assert(matches != NULL);

    cptr_match = 0;
    for (int i = 0; i < count; i++)
    {
        if (strcmp(propositions[i].cde, pcde) == NULL)
        {
            matches[cptr_match] = propositions[i];
            cptr_match++;
        }
    }
    *match_count = cptr_match;
    return matches;
}

在其他一些功能中,我有:

proposition *matches =
    get_matching_propositions(current_question.cde, propositions, &match_count);
free(matches);

然后程序崩溃了此消息:
返回的过程-1073740940(0xc0000374)执行时间:1.370 s。

I have this function which is using calloc to create an array of structure proposition. Whenever I try to free the resulting array from another fuction, it crashes.

proposition* get_matching_propositions(char *pcde, proposition *propositions, int *match_count)
{    
    proposition *matches;
    int         count       = get_count_from_stream(ptf_proposition, sizeof(proposition)),
                cptr_match  = 0;

    for (int i = 0; i < count; i++)
    {
        if (strcmp(propositions[i].cde, pcde) == NULL)
        {
            cptr_match++;
        }
    }

    matches = (proposition*) calloc (cptr_match, sizeof(propositions));
    assert(matches != NULL);

    cptr_match = 0;
    for (int i = 0; i < count; i++)
    {
        if (strcmp(propositions[i].cde, pcde) == NULL)
        {
            matches[cptr_match] = propositions[i];
            cptr_match++;
        }
    }
    *match_count = cptr_match;
    return matches;
}

Inside some other function I have:

proposition *matches =
    get_matching_propositions(current_question.cde, propositions, &match_count);
free(matches);

Then the program crashes with this message :
Process returned -1073740940 (0xC0000374) execution time : 1.370 s.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文