2016-08-12 2 views
1

시도 프로그램을 실행할 때 계속 오류가 발생합니다.시도 : 왜 malloc 오류가 발생했는지

의 a.out : malloc.c를 : 2,372 : sysmalloc 어설`(old_top == (((mbinptr) (((숯 *) & ((AV) -> 빈 [((1) - (1)) * 2])) - __builtin_offsetof (struct malloc_chunk, fd))))) & & old_size == 0) || ((unsigned long) (old_size)> = (unsigned long) ((__ builtin_offsetof (struct malloc_chunk, fd_nextsize)) + ((2 * (sizeof (size_t)) -1)) & ~ (old_top) -> 크기 & 0x1) & & & ((부호없는 길이) old_end & 페이지 마스크 == 0) '실패했습니다.

내 코드는 다음과 같습니다

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

typedef struct nodeData 
{ 
    char ch;    /* This structure looks like a linked list */ 
    struct nodeData *next; 
}node; 

typedef struct tries 
{ 
    node *start[26]; 
}tries; 

tries *makeAllNull(tries *root) 
{ 
    int i=0; 
    for(i=0;i<=26;i++) 
    { 
     root->start[i] = NULL; 
    } 
    return root; 
} 

/* Insert the given string in to the tries */ 
tries *insert(tries *root,char *str,int len) 
{ 
    int i=0; 
    tries *temp; 
    temp = (tries *)malloc(sizeof(tries)); 
    while(i<len) 
    { 
     int k = str[i] - 'a'; 
     temp->start[k] = (node *)malloc(sizeof(struct nodeData)); 
     temp->start[k]->ch = str[i]; 
     temp->start[k]->next = NULL; 
     if(temp->start[k] == NULL) 
     { 
      root->start[k] = temp->start[k]; 
     } 
     else{ 
       root->start[k]->next = temp->start[k]; 
      } 
     i++; 

    } 
    return root; 
} 

int main() 
{ 
    int i=0; 
    tries *root; 
    root = (tries *)malloc(sizeof(node *)); 
    makeAllNull(root); 
    char str[30]; 
    while(i<5) 
    { 
     scanf("%s",str); 
     root = insert(root,str,strlen(str)); 
    } 
    return 0; 
} 
+2

'루트 = (시도해 *) malloc (크기 (노드 *)),'->'루트 = malloc (sizeof (* 루트));','i <=26' -->'i <26' – BLUEPIXY

+0

' 구조가 필요하지 않습니다. 'trie' 구조체에는 유효한 단어 끝을 나타내는 멤버가 있어야합니다. 그리고 다른 멤버는 더 많은'trie' 구조체에 대한 포인터의 배열이어야합니다. – user3386109

+0

오류 메시지에는 메모리가 손상되었다고 알려주는 것 이외의 유용한 정보가 없습니다. – user3386109

답변

0

귀하의 if(temp->start[k] == NULL) 거의 항상 false malloc 후이 라인

root->start[k]->next = temp->start[k]; 

에서 역 참조 NULL 포인터로 연결 그리고 이것은 메모리를 손상하는 것입니다.

if(temp->start[k] == NULL) 대신 if(root->start[k] == NULL)을 의미 했습니까?

+0

이 오류가 없습니다. temp = (tries *) malloc (sizeof (tries)); –

관련 문제