尝试在哈希表中插入键时收到“分段错误”

时间:2019-08-02 01:40:16

标签: c memory hash linked-list hashtable

我正在尝试使用已经完成的链接列表结构在C中实现开放式哈希。链表结构可以完美地工作,但是当尝试在哈希表结构中使用它们时,我会不断收到“分段错误(核心已转储)”

我已经检查过以确保我使用了正确的数据类型,并且分配了正确的内存量。

{
  "name": "Reddit Hide Username",
  "version": "1.0",
  "description": "Hide your username in new Reddit UI!",
  "permissions": [
    "activeTab",
    "declarativeContent",
    "storage"
  ],
  "options_page": "options.html",
  "background": {
    "scripts": [
      "background.js"
    ],
    "persistent": false
  },
  "page_action": {
    "default_popup": "popup.html",
    "default_icon": {
      "16": "images/icon16.png",
      "32": "images/icon32.png",
      "48": "images/icon48.png",
      "128": "images/icon128.png"
    }
  },
  "icons": {
    "16": "images/icon16.png",
    "32": "images/icon32.png",
    "48": "images/icon48.png",
    "128": "images/icon128.png"
  },
  "manifest_version": 2
}

createTable()不会引发任何运行时错误。但是任何其他HashTable函数都可以。我无法访问表中的链接列表。

1 个答案:

答案 0 :(得分:0)

Rslt是一个指针。您不能像初始化struts那样初始化它。从函数返回后,rslt指针在以下代码中将变为无效

 HashTable* createTable(){
     HashTable* rslt = {calloc(9,sizeof(LinkedList*))};

{..}不是动态内存分配。尝试先分配rslt

     HashTable *rslt = malloc(sizeof(HashTable));
     rslt->table = calloc...