2017-11-25 28 views
1

런타임시 디버그 어설 션이 실패합니다.힙 손상 - ​​디버그 어설 션이 실패했습니다. 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 
} 
+0

당신은'free (uf-> parent)없이 프로그램을 실행하려고 할 수 있습니까? –

+1

'union_find_t;'는 포인터의 typedef이므로'malloc (sizeof (union_find_t));'는 포인터를위한 공간을 할당하고, 구조체가 아닙니다. typedef에서'*'를 제거해야하는 것처럼 보입니다. –

+0

@BoPersson - 실제로 당신의 해결책이 더 좋습니다. 비록 typedef 구조체 union_find_t { int * parent; int * rank; int 구성 요소. } union_find_t; 좀 이상하게 보입니다. –

답변

1

이 :

typedef struct union_find_t 

에 대한 typedef입니다 :

이 작업을 수행 할 때

그래서 :

malloc(sizeof(union_find_t)); 

당신은 그냥 당신이 필요로하는 구조체를 들어, 그 구조체에 포인터을위한 공간을 할당! 대신

malloc(sizeof(struct union_find_t)); 

:

함께보십시오.

+1

그것은 까다 롭고 미묘하다 - 고마워! –