런타임시 디버그 어설 션이 실패합니다.힙 손상 - 디버그 어설 션이 실패했습니다. in dbgheap.c line 1322 expression _crtIsValidHeapPointer (pUserData)
in dbgheap.c line 1322 expression _crtIsValidHeapPointer(pUserData)
디버거에서 실행하면 아래 표시된 줄에서 중단 점이 발생합니다.
이 할당/할당 해제 오류를 어떻게 해결할 수 있습니까?
내가 헤더 파일에이 개 기능이 있습니다
struct union_find_t;
struct union_find_t* union_find_init(int n);
void union_find_free(struct union_find_t* uf);
을하고이 .c에서이 두 기능 구현을 파일입니다
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
}
당신은'free (uf-> parent)없이 프로그램을 실행하려고 할 수 있습니까? –
'union_find_t;'는 포인터의 typedef이므로'malloc (sizeof (union_find_t));'는 포인터를위한 공간을 할당하고, 구조체가 아닙니다. typedef에서'*'를 제거해야하는 것처럼 보입니다. –
@BoPersson - 실제로 당신의 해결책이 더 좋습니다. 비록 typedef 구조체 union_find_t { int * parent; int * rank; int 구성 요소. } union_find_t; 좀 이상하게 보입니다. –