![]() |
#2
叶纤2020-01-21 00:06
|

基本上是书上的代码,用的是 马克艾伦维斯的《数据结构与算法分析》
触发的警告和触发警告的位置在下面
头文件代码如下(省去了各函数原型)

typedef unsigned long long Index;
typedef char* eletype;
typedef struct HashCell Cell;
enum CellInfo {legitimate, Empty, Delete};
struct HashCell
{
eletype ele;
enum CellInfo Info;
};
typedef struct HashRecord* HashTable;
struct HashRecord
{
int tableSize;
Cell* theCells;
};
触发警告的函数如下

HashTable initializeHash(int tableSzie)
{
HashTable hashtbl = (HashTable)malloc(sizeof(struct HashRecord));
if (tableSzie < MINSIZE) Error("initialize hash");
if (hashtbl)
{
hashtbl->tableSize = NextPrime(tableSzie);
hashtbl->theCells = (Cell*)malloc(sizeof(Cell) * hashtbl->tableSize);
if (hashtbl->theCells)
{
for (int i = 0; i < hashtbl->tableSize; i++)
{
/*
警告 C6386 写入到“hashtbl->theCells”时缓冲区溢出:
可写大小为“sizeof(Cell)*hashtbl->tableSize”个字节,但可能写入了“16”个字节。
*/
hashtbl->theCells[i].ele = NULL;//此处触发以上警告
hashtbl->theCells[i].Info = Empty;
}
}
else Error("initialize hash 1");
}
else Error("initialize hash 2");
return hashtbl;
}