gpt4 book ai didi

c - 进入 insertHash 函数后我的代码崩溃了

转载 作者:太空宇宙 更新时间:2023-11-04 08:08:39 24 4
gpt4 key购买 nike

我下面的代码是获取一个文本文件并对其进行哈希处理,键是英文单词,值是西类牙语翻译。

我的代码在进入函数 insertHash 时崩溃,特别是下面的 while 循环

while(hashTable[hashIndex].marker == 1)
{
printf("%d loop of hash index \n", i);
hashIndex = (hashIndex+1)%tableSize;
i++;
}

请忽略 i++ 和 printf,它们用于调试目的。
函数本身甚至没有经过一次迭代,就崩溃了。
感谢任何帮助,代码如下:

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

int numofEle = 0;
const int STEPSIZE = 100;
//Hash table structure
struct node *hashTable;
struct node{
int marker;
char *key;
char *value;
};

//function declaration
char **loadfile(char *filename, int *len);

//Hash function for strings;
unsigned long hash(unsigned char *str)
{
unsigned long hash = 5381;
int c;

while (c = *str++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

return hash;
}
//Function to insert elements in hash table
void insertHash(int len, char *words){
printf("Just entered insert hash function \n");
int hashKey, hashIndex;
int tableSize = len;
printf("before hashing\n");
hashKey = hash(words);
printf("after hashing\n");
hashIndex = hashKey % tableSize;
printf("After hash index \n");
if(numofEle == tableSize)
{
printf("The hash table is full\n.");
return;
}
printf("before checking for marker\n");
while(hashTable[hashIndex].marker == 1)
{
hashIndex = (hashIndex+1)%tableSize;
}
printf("After checking index \n");
strcpy(hashTable[hashIndex].key, strtok(words, "\t"));
strcpy(hashTable[hashIndex].value, strtok(NULL, "\t"));
hashTable[hashIndex].marker = 1;
printf("After assigning variables to table \n");
numofEle++;

return;
}
//Function to search for element
void searchElement(char *key, int len, int *totalProbe)
{
int count = 0;
int probe = 0;
int flag = 0;
char search;
int hashKey = hash(key);
int tableSize = len;
int hashIndex = hashKey % tableSize;

if(numofEle == 0)
{
printf("Hash Table is empty\n.");
return;
}
printf("Probes| keys\n");
printf(" %d| 0\n", probe);

while(hashTable[hashIndex].marker != 0 && count <= tableSize)
{
probe++;
if(hashTable[hashIndex].key == key)
{
printf("The word %s ", key);
printf("translate to %s.\n", hashTable[hashIndex].value);
flag = 1;
break;
}
else
{
printf(" %d| %d\n", probe, hashIndex);
}
hashIndex = (hashIndex+1) % tableSize;
}

if(!flag)
{
printf("Given data is not here\n.");
}

*totalProbe = probe;
return;
}

int main(int argc, char *argv[])
{
//Check for readable input
if(argc == 1)
{
fprintf(stderr, "Must supply a file name to read.\n");
exit(1);
}
int i, totalProbe;
int len = 0;
//Function to read text into array
char **words = loadfile(argv[1], &len);
printf("After loading files\n ");
//allocating memory for hash table and elements
struct node *hashTable = malloc(sizeof(struct node)* len);
printf("After struct allocation\n");
for(i = 0; i < len; i++)
{
hashTable[i].key = malloc(300 * sizeof(char));
hashTable[i].value = malloc(300 * sizeof(char));
hashTable[i].marker =(int*)malloc(sizeof(int));
}
for(i = 0; i < len; i++)
{
hashTable[i].marker = 0;
}
printf("After allocation for table elements\n");
//Loop to hash and insert elements
int totalHash = len;
for(i = 0; i < len; i++)
{
insertHash(len, words[i]);
totalHash--;
}
printf("After hashing words");
char input[100];
printf("Hash Table:\n");
printf("total NOT hashed: %d out of %d\n", totalHash, len);

printf("Please enter key: \n");
scanf("%s", input);

searchElement(input, len, &totalProbe);
printf("max_run of probes = %d\n", totalProbe);

return (EXIT_SUCCESS);
}

//Reads large files into array
char **loadfile(char *filename, int *len)
{
FILE *fp = fopen(filename, "r");
if(!fp)
{
fprintf(stderr, "Can't open %s for reading.\n", filename);
return NULL;
}

int arrlen = STEPSIZE;

//Allocate space for 100 char pointers.
char **lines = (char**)malloc(arrlen*sizeof(char*));

char buff[10000];
int i = 0;

while(fgets(buff, 10000, fp))
{
//Check for full array, if so, extend the array
if(i == arrlen)
{
arrlen += STEPSIZE;
char **newlines = realloc(lines, arrlen*sizeof(char*));
if(!newlines)
{
fprintf(stderr, "Can't reallocate\n");
exit(1);
}
lines = newlines;
}
// trim off \0 at the end
buff[strlen(buff)-1] = '\0';
//get length of buffer
int blen = strlen(buff);

char *str = (char*)malloc((blen+1)*sizeof(char));

//copy string from buff to structure
strcpy(str, buff);

lines[i] = str;

i++;
}

*len = i; //Set length of char pointers
return lines;
}

最佳答案

两件事:

1.需要在结构定义下面声明struct node *hashTable

//Hash table structure    
//struct node *hashTable; <--- incorrect
struct node{
int marker;
char *key;
char *value;
};
struct node *hashTable;

2.您正在 main() 中重新声明 struct node *hashTable

struct node *hashTable = malloc(sizeof(struct node)* len);

这个指针的作用域是局部的。

您已经全局声明了 struct node *hashTable

使用:

hashTable = (struct node *)malloc(sizeof(struct node)* len);

3.为什么要为marker动态分配内存?它是 struct node 的一个 int 成员。它有 sizeof(int) 字节的内存。

hashTable[i].marker =(int*)malloc(sizeof(int)); ---> 不需要。你可以评论它。

关于c - 进入 insertHash 函数后我的代码崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40968289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com