堆损坏-调试断言失败.在dbgheap.c第1322行中的表达式_crtIsValidHeapPointer(pUserData) [英] Heap corruption - debug assertion failed. in dbgheap.c line 1322 expression _crtIsValidHeapPointer(pUserData)
本文介绍了堆损坏-调试断言失败.在dbgheap.c第1322行中的表达式_crtIsValidHeapPointer(pUserData)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在运行时我得到调试断言失败.
At runtime I get debug assertion failed.
in dbgheap.c line 1322 expression _crtIsValidHeapPointer(pUserData)
如果我在调试器中运行,则会在下面所示的行中触发一个断点.
If I run in a debugger I get a breakpoint triggered in line shown below.
如何解决此分配/取消分配错误?
How can I fix this allocation/de-allocation error?
我在头文件中有2个函数:
I have 2 functions in a header file:
struct union_find_t;
struct union_find_t* union_find_init(int n);
void union_find_free(struct union_find_t* uf);
.在.c文件中,这两个功能的实现为:
and in the .c file the implementation for these 2 functions is:
typedef struct union_find_t {
int* parent;
int* rank;
int components;
} *union_find_t;
struct union_find_t* union_find_init(int n) {
struct union_find_t* uf = malloc(sizeof(union_find_t));
uf->parent = malloc(n * sizeof(int));
uf->rank = malloc(n * sizeof(int));
uf->components = n;
for (int i = 0; i < n; ++i) {
uf->parent[i] = i;
uf->rank[i] = 0;
}
return uf;
}
void union_find_free(struct union_find_t* uf) {
free(uf->parent);
free(uf->rank);
free(uf); //*** breakpoint triggered here
}
推荐答案
此:
typedef struct union_find_t
是以下类型的定义:
*union_find_t;
因此,当您执行此操作时:
So when you do this:
malloc(sizeof(union_find_t));
您只需为指向该结构的指针分配空间,而不为您需要的结构分配空间!
you just allocate space for a pointer to that struct, not for a struct as you need to!
尝试:
malloc(sizeof(struct union_find_t));
相反.
这篇关于堆损坏-调试断言失败.在dbgheap.c第1322行中的表达式_crtIsValidHeapPointer(pUserData)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文