C 运行无结果,断点提示堆损坏
本帖最后由 ing 于 2020-3-5 16:44 编辑我的运行没有任何结果的输出
我很确定我的代码没错,当我断点调试按下F11时发生了下图
```
#include<malloc.h>
#include<stdio.h>
#include<stdbool.h>
#define VType int
#define Hash_Size 20
#define NULL_KEY -1
typedef struct
{
int *elem;
int length;
}HashTable;
void init(HashTable *t, int length)
{
t->elem = (int*)malloc(Hash_Size*sizeof(int));
t->length = length;
for (size_t i = 0; i < Hash_Size; i++)
{
t->elem = NULL_KEY;
}
}
int hash(int a)
{
return a % 13;
}
void insert(HashTable **t,int a)
{
int hashAddress = hash(a);
while ((*t)->elem != NULL_KEY)
{
hashAddress = (hashAddress + 1) % 13;
}
(*t)->elem = a;
}
int search(HashTable *t, int a)
{
int hashAddress = hash(a);
while (t->elem != a)
{
hashAddress = (hashAddress + 1) % 13;
if (hashAddress == hash(a))
{
return -1;
}
}
return hashAddress;
}
int main(int argc, char* argv[])
{
HashTable *t = (HashTable*)malloc(Hash_Size*sizeof(HashTable));
init(t,10);
int arr = { 12,23,45,57,20,03,78,31,15,36 };
for (size_t i = 0; i < t->length; i++)
{
insert(&t,arr);
}
int result = search(t, 12);
if (result != NULL_KEY)
{
printf("在哈希表的位置是:%d",result);
}
else
printf("哈希表不存在此元素");
free(t->elem);
free(t);
}
``` 解决了,重新打开编译器运行。。。
页:
[1]