이 함수는 사전에서 단어를 읽고 링크 된 목록의 해시 테이블에 넣는 "load"함수입니다. 행을 읽고 new_node-> 텍스트에 저장하려고하면 컴파일러가 분할 오류를 반환하고 이유를 모르겠습니다. strncpy를 사용하면 오류가 발생합니다.strncpy의 분할 오류 - 사전로드
#define HASHTABLE_SIZE 76801
typedef struct node
{
char text[LENGTH+1];
//char* text;
//link to the next word
struct node* next_word;
}
node;
node* hashtable[HASHTABLE_SIZE];
bool load(const char* dictionary)
{
FILE* file = fopen(dictionary,"r");
unsigned long index = 0;
char str[LENGTH+1];
if(file == NULL)
{
printf("Error opening file!");
return false;
}
while(! feof(file))
{
node * new_node = malloc(sizeof(node)+1000);
while(fscanf(file,"%s",str) > 0)
{
printf("The word is %s",str);
strncpy(new_node->text,str,LENGTH+1);
//strcpy(new_node->text,str);
new_node->next_word = NULL;
index = hash((unsigned char*)new_node->text);
if(hashtable[index] == NULL)
{
hashtable[index] = new_node;
}
else
{
new_node->next_word = hashtable[index];
hashtable[index] = new_node;
}
n_words++;
}
//free(new_node);
}
fclose(file);
loaded = true;
return true;
}
'strncpy'는 문자열을 종료하지 않을 수 있으므로주의해야합니다. 그리고 노드에 1000 바이트를 추가로 할당하는 이유는 무엇입니까? –
또한 디버거를 사용하여 충돌이 발생한 위치를 확인하십시오. 또한 호출 스택을보고 충돌이 어디에서 발생했는지 확인하고 충돌 원인을 파악하는 데 도움이되는 변수를 검토 할 수 있습니다. –
'node * new_node = malloc (sizeof (node) +1000);'무엇? 왜 여분의 1000 바이트? –