【问题标题】:Reading array within a struct causes a segmentation fault在结构中读取数组会导致分段错误
【发布时间】:2019-01-21 15:42:40
【问题描述】:

我创建了一个链表,每个节点都包含一个结构作为元素和一个指向下一个节点的指针,如下所示

list.h

typedef struct node {
        group data;
        struct node *next;
} node;

typedef struct group {                                  
        unsigned int elements_count;                    
        unsigned int closed;                            
        unsigned int members[4];                        
} group;

list.c

node *add(node *head, group toadd) {
    node *n_node = (node*) malloc(sizeof(node));
    if(n_node != NULL) {
        n_node->next = head;
        group *n_group = &n_node->data;
        /* Copy the values of the group into the created node */
        n_group->elements_count = toadd.elements_count;
        n_group->closed = toadd.closed;
        for(int i  = 0; i < 4; i++) 
            n_group->members[i] = toadd.members[i];
    } 
    else {
        throw_error("malloc returned a NULL pointer");
    }
    return n_node;
}

当我尝试读取数组的第一个元素 (node-&gt;data.members[0]) 时出现问题。 Valgrind 说,问题在于地址未被堆栈、malloc 或(最近)释放的大小为 4 的无效读取。 为什么即使我使用 malloc 分配每个节点也会出现分段错误?

编辑:

ma​​in.c

node *group_list = NULL;

/* Other code here.. */

group *cur_group = is_present(group_list, msg_gest.mtype);

if(cur_group == NULL) {
    // The group isn't still present in the group list, then add it
    group new_group = {
        .elements_count = 0,
        .closed = 0,
        .members = {-1, -1 , -1, -1}
    };

    new_group.members[new_group.elements_count++] = msg_gest.mtype;
    new_group.members[new_group.elements_count++] = msg_gest.to_add;
    new_group.closed = msg_gest.to_close;
    group_list = add(group_list, new_group);
} else {
    cur_group->members[cur_group->elements_count++] = msg_gest.to_add;
    cur_group->closed = msg_gest.to_close;
}

is_present

group* is_present(node *head, int matr) {

        group *c_group;
        node *c_node = head;

        while(c_node != NULL) {

                c_group = &c_node->data;


                if(*(c_group->members) == matr) // !!Segmentation fault is caused by this read
                         return c_group;
                printf("\n%d", *(c_group->members));

                c_node = c_node->next;
        }
        return NULL;
}

【问题讨论】:

  • 您发布的代码看起来不错。你能告诉我们minimal reproducible example,以便我们更好地帮助你吗?
  • edit您的问题并提供minimal reproducible example。问题可能出在调用add 的代码中。另请阅读:minimal reproducible example
  • 请显示调用你的函数add()的代码。
  • 请阅读minimal reproducible example 是什么并提供一个。你刚刚提供了一堆函数。提供一个可以编译并重现问题的 c 文件。这需要您做一些工作,但这是为免费咨询付出的代价。
  • 遗憾的是,我无法构建一个最小、完整和可验证的示例,因为这个问题只发生在这个项目中。我发现如果我删除 return c_group 语句,问题就解决了。

标签: c unix memory struct


【解决方案1】:

我认为问题是由堆溢出引起的,为了解决它,我修改了节点结构如下

typedef struct node {
        group* data;
        struct node *next;
} node;

我像这样在add 函数中分配了组

n_node->data = (group*) malloc(sizeof(group));

【讨论】:

    【解决方案2】:

    换行试试

    if(*(c_group-&gt;members) == matr)

    if(c_group-&gt;members[0] == matr)

    【讨论】:

    • 它没有改变任何东西,因为*(c_group-&gt;members)c_group-&gt;members[0] 指的是同一个内存位置。
    猜你喜欢
    • 1970-01-01
    • 2021-03-19
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多