malloc으로 동적할당한 메모리 해제시 세그멘테이션 에러..

KimJH의 이미지

안녕하세요..

#include
#include

int main()
{
int cnt;
struct ab** x;

x = (struct ab**)malloc(10 * sizeof(struct ab*));
for(cnt=0;cnt<10;cnt++) x[cnt] = (struct ab*)malloc(10 * sizeof(struct ab));

for(cnt=0;cnt<10;cnt++) free(x[cnt]);
free(x);

return 0;
}

이중 구조체 동적할당인데요
여기서

for(cnt=0;cnt<10;cnt++) free(x[cnt]);
free(x);
이 부분 때문에 리눅스에서 세그멘테이션 오류가 납니다
배열 범위는 맞는 것 같은데 왜 그럴까요?

khj776의 이미지

제가 이름만 바꿨는데 이상없던데여~

int main()
{
int cnt;
node **ptr;

ptr=(node **)malloc(10*sizeof(node *));
for(cnt=0;cnt<10;cnt++)
ptr[cnt]=(node *)malloc(sizeof(node)*10);
for(cnt=0;cnt<10;cnt++)
free(ptr[cnt]);
free(ptr);
return 0;
}