我得到了这个代码,它创建了一个3D ARRAY - 更像是一个2D数组,每个单元格都是一个指针。我尝试将其初始化为LENGTH_OF_ROW * SIZE * sizeof(Node)
。
这是结构节点:
typedef struct Node
{
void * data;
void *key;
}Node;
这是创建哈希表的代码:
Node*** createHashTable(size_t size)
{
Node ***hashTable = (Node***)malloc(LENGTH_OF_ROW*sizeof(Node**));
for (int i = 0; i < LENGTH_OF_ROW; i++)
{
hashTable[i] = (Node**)malloc(size * sizeof(Node*));
for (int j = 0; j < (int)size; j++)
{
hashTable[i][j] = malloc(sizeof(Node));
hashTable[i][j]->key = EMPTY_CELL;
}
}
return hashTable;
}
这是valgrind错误:
49427 ==条件跳转或移动取决于未初始化的值
49427 ==在0x4C28BCD:malloc(vg_replace_malloc.c:296)
49427 == by 0x400844:createHashTable(GenericHashTable.c:40)
49427 == by 0x40091B:createTable(GenericHashTable.c:57)
49427 == by 0x4015E5:main(HashIntSearch.c:30)
第40行的错误,
这一行:hashTable[i] = (Node**)malloc(size * sizeof(Node*));
无法弄清楚为什么我会收到此错误。
谢谢!
大小以这种方式定义:
size_t tableSize;
int val;
sscanf(argv[1], "%d",(&tableSize));
sscanf(argv[2], "%d", &val);
// Creating a table with addresses of functions.
TableP table = createTable(tableSize, &cloneInt, &freeInt, &intFcn,
&intPrint, &intPrint, &intCompare);
这是createTable:
TableP createTable(size_t tableSize, CloneKeyFcn cloneKey, FreeKeyFcn freeKey
, HashFcn hfun, PrintKeyFcn printKeyFun, PrintDataFcn printDataFun
, ComparisonFcn fcomp)
{
TableP table = (TableP)malloc(sizeof(Table));
table->multiplicity = 1;
table->originalSize = tableSize;
table->currentSize = tableSize;
table->hashTable = createHashTable(tableSize);
table->cloner = cloneKey;
table->freer = freeKey;
table->hash = hfun;
table->keyPrinter = printKeyFun;
table->dataPrinter = printDataFun;
table->comparer = fcomp;
return table;
}
我正在运行大小为1的valgrind