小编Xav*_*bec的帖子

循环分配内存时的分段错误

我在C中创建一个哈希表,其键的类型为char*.我将密钥存储在表本身中的原因超出了此问题的范围.哈希表大部分都在工作,除了以下问题:当表大小超过2112个元素并且我尝试将键初始化为NULL指针时,我遇到了分段错误.

这是hashTable的定义:

typedef struct hash_table
{
  uint32_t size;  // # of elements the table can store
  uint32_t count; // # of elements in the table
  char **keys;    // The pointer to the first key. Each key is a char*
  int32_t *vals;  // The pointer to the first val.
} hashTable;
Run Code Online (Sandbox Code Playgroud)

这里是我用NULL指针作为键初始化表的地方:

// Declare the pointer to the hash table
hashTable *symbolTable = malloc(sizeof(hashTable));

// Set the hash table properties
symbolTable->size = 7699;
symbolTable->count = 0;
symbolTable->keys = malloc(sizeof(symbolTable->keys[0]) * symbolTable->size); …
Run Code Online (Sandbox Code Playgroud)

c malloc segmentation-fault

0
推荐指数
1
解决办法
69
查看次数

标签 统计

c ×1

malloc ×1

segmentation-fault ×1