C 未定义行为 - 单链表

标签 c gdb singly-linked-list

我已经实现了一个单链表,我注意到了非常奇怪的行为,但无法查明它发生的确切原因。我已经尝试使用 gdb 找出问题所在,看起来每当我计算列表的大小时,事情就开始出错了。这是我用来测试我的实现的程序,下面是实际的实现。

#include <stdio.h>
#include "singlylinked.h"

slist initialize(void); /* initializes test singly linked list */

slist initialize(){
    int i, a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    slist l = sl_alloc();
    int a_size = sizeof(a)/sizeof(a[0]);
    for(i=0;i<a_size;i++){
        sl_add(l, (void *)&a[i]);
    }
    return l;
}

int main(){
    slist l = initialize();
    int i, size = sl_size(l);
    for(i = 0; i < size; i++){
        printf("%d ", *(int *)sl_get(l,i));
    }
    fputc('\n',stdout);
    return 0;
}

现在是真正的实现,我只贴出测试中使用的方法:

/* allocates an empty slist */
slist sl_alloc(){
    return (slist) calloc(1, sizeof(struct node));
}

/* adds val to linked list */
void sl_add(slist l, void *val){
    node *current, *new;
    new = calloc(1, sizeof(struct node));
    new->content = val;
    new->next = NULL;
    if((current = *l)){
        while(current->next){
            current = current->next;
        }
        current->next = new;
    } else {
        *l = new;
    }
}

/* returns ith value in singly linked list l */
void *sl_get(slist l, int i){
    node *current;
    int j; /* counter */
    void *result = NULL;
    if((current = *l)){
        int size = sl_size(l);
        if(i < size){
            for(j = i; j > 0; j--){
                current = current->next;
            }
            result = current->content;
        } 
    }
    return result;
}

/* returns the size of the singly linked list */
int sl_size(slist l){
    int size = 0;
    node *current;
    if((current = *l)){
        do {
            size++;
            current = current->next;
        } while (current);
    }
    return size;
}

现在这就是我定义 sliststruct node 的方式:

typedef struct node **slist;

typedef struct node {
    void *content;
    struct node *next;
} node;

编辑:奇怪的行为是这样的:当我尝试打印出来时,它在列表中打印出错误的值。当我使用 gdb 运行程序时,在第一次调用 sl_size 后开始发生这种情况。

最佳答案

问题是您对列表的初始化。

您将数组 a[] 的 10 个元素添加到您在 initialize() 中创建的列表中。唯一的问题是您在列表节点中存储了指向数组 a[] 中数据的指针。不幸的是,这个数组是函数的本地数组!一旦从 initialize() 返回,这个数组就不再有效,指针也不再指向有效位置。因此,您期望指向的数字将被“垃圾”值取代。

关于C 未定义行为 - 单链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34958010/

相关文章:

c++ - 在 LinkedList 中插入节点会出现段错误

java - java将两个链表合并成新的链表

c - 产品 key 的实现

c++ - 我所有变量的运行时检查失败 #2 错误

linux - gdb - 目标中不支持硬件断点

debugging - 您使用/编写过哪些有用的 GDB 脚本?

std::string 函数中的 C++ 段错误

c - 链表头无法指向旧元素

c - Linux 调度 : OS vs "virtual"

flex 和 bison 的通用标记