在Calloc创建的结构数组上使用Free()时,程序会崩溃
我有此功能,它使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论