【问题标题】:Getting a Segmentation Fault 11 Not Sure if Using Pointers Correctly in C获得分段错误 11 不确定在 C 中是否正确使用指针
【发布时间】:2017-03-04 03:04:17
【问题描述】:

请记住,我是 C 新手,整个指针/内存分配对我来说有点棘手。通过终端输入的命令行参数也是如此。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct node {
    long long key;
    long long val;
    struct node *next;
}; 
struct hashTable {
    struct node *head;
    int buckets;
}; 

int count = 0;

struct hashTable *newTable;

//create a new node
struct node * createNode (long long key, long long val) {

    struct node *newNode;
    newNode = (struct node *) malloc(sizeof(struct node));

    newNode -> key = key;
    newNode -> val = val;
    newNode -> next = NULL;

    return newNode;
}

//insert data into Hash
void insertToHash(long long key, long long val) {
    // hash func
    int hashIndex = key % 1000, inTable = 0;
    struct node *newNode = createNode(key, val);

    //traversal nodes
    struct node *temp, *curr;
    curr = newTable[hashIndex].head;

    //if the table at given index is empty 
    if (newTable[hashIndex].head == NULL) {
        newTable[hashIndex].head = newNode;
        count ++;
        return;
    }

    temp = curr;
    //if key is found break, else traverse till end
    while(curr != NULL) {
        if (curr -> key == key) {
            inTable = 1;
            free(newNode); //since already in the able free its memory
            break;
        }
        else {
            temp = curr;
            curr = curr->next;
        }
    }

    if (inTable == 1) {
        printf("Address is already in the table");
    }
    //key not found so make newNode the head
    else {
        newNode -> next = newTable[hashIndex].head;
        newTable[hashIndex].head = newNode;
        count ++;
    }

}

//initialize hashtable to 1000 entries
struct hashTable * createHashTable (int buckets) {

    int i;
    for(i=0; i<buckets; i++) {
        newTable[i].head = NULL;
    }

    return newTable;
}



int main(int argc, char *argv[]) {

    createHashTable(1000);




}

因此,当我搜索 Segmentation Fault 11 是什么时,我发现它与无法访问某些内存有关。我假设我的问题与初始化表 newTable 以及未正确使用指针或为其正确分配内存有关。请记住,这是我第一次真正尝试在 C 中创建数据结构,所以看起来很明显的事情对我来说并不明显。

【问题讨论】:

  • struct hashTable *newTable; : newTableNULL。不能像newTable[i].head = NULL;一样使用
  • 关于您的次要但不相关的问题,请参阅What are the arguments to main() for?
  • 首先,您没有为newTable 分配任何空间。你需要malloc 一些空间newTable .. 虽然我猜你真正想要的是1 struct hashTable 和1000 struct nodes?在这种情况下,我不会将 newTable 设为指针,只需使用 struct hashTable newTable;,然后将 malloc 设为您想要的 struct nodes 的数量。
  • @yano 这是正确的,我只需要一个带有 1000 个桶的 HashTable 并使用链接进行冲突。但是,如果我按照您的建议进行操作并分配我需要的结构节点的数量,例如,如果我没有将 Hashtable 设置为数组,我将如何输入一个值来表示 hashIndex 50?
  • 您可以像索引数组一样索引指针。您已经这样做了,但没有分配内存。

标签: c pointers segmentation-fault malloc hashtable


【解决方案1】:

您的代码布局很棒且易于阅读!

以下是错误的

for(i=0; i<buckets; i++) {
    newTable[i].head = NULL;
}

基于

struct hashTable *newTable;

newTable 是指向单个结构的指针,而不是指向数组的指针。

至于正确的解决方案,请先按照 K&R 书中的 hashtable 示例,然后随意修改以满足您的需要。

【讨论】:

  • 只要分配了足够的内存就可以了。但是编译器不知道要索引多少元素。如果你为一个元素分配内存,它会让你索引第 100 个,但错误只发生在运行时。只是因为newTable 是一个全局初始化变量(到NULL!),编译器才不会发出警告。
  • @weatherVane 如果我想 malloc 1000 个桶,那我该怎么做?这让我很困惑。
  • newTable = malloc(1000 * sizeof *newTable); if(newTable == NULL) { /* handle error */ }。但是由于您已经硬编码了 1000 并且您有一个全局 newTable 指针,我不知道您为什么不只是 #define ELEMS 1000 然后是 struct hashTable newTable[ELEMS]; 并完成它。
  • @WeatherVane 这是我使用 C 的第三天,我不确定#define 是做什么的。你能详细说明一下吗?
  • 哦!请read this 了解 C 预处理器。本质上是文本替换。在代码中遇到ELEMS 的任何地方都将其替换为1000(在文本上)。这意味着,如果您对数字进行硬编码,您只需在您可以找到并根据需要进行修改的地方执行 一次,整个程序都会效仿。
猜你喜欢
  • 2012-04-01
  • 1970-01-01
  • 2014-12-19
  • 2012-11-06
  • 2019-06-02
  • 1970-01-01
  • 2019-11-22
  • 1970-01-01
  • 2018-04-18
相关资源
最近更新 更多