c - 为什么我的代码会给我垃圾值,有时还会出现段错误?

标签 c segmentation-fault

我正在尝试在给定 2 个已排序链表的情况下实现 merge(),并输出一个合并这两个链表并进行排序的链表。我首先创建一个数组并将项目放在那里,但是当我打印数组元素时,我得到垃圾值/段错误。我知道要完成很多代码,但我真的很感激任何能提供帮助的人:)

typedef struct _node {
    int data;
    struct _node * next;
} node_t;

typedef struct {
    node_t * head;
    node_t * tail;
} LL_t;

LL_t* createList(int num_nodes);
void printList(LL_t* L);
void merge(LL_t * L, LL_t * L2);

void merge(LL_t * L, LL_t * L2){
    if(L2->head==NULL){ // empty L2
        free(L2);
        return;
    }
    else if(L->head==NULL){ // empty L1
        *L=*L2;
        free(L2);
        return;
    }

    node_t* node=L->head;
    int mid=0;
    if(node->next!=NULL){
    for (mid=0; node->next!=NULL; mid++) //finds last index of L1
        node=node->next;
    }

    L->tail->next=L2->head;
    L->tail=L2->tail;
    node_t* ind = L->head;
    free(L2);
    int len=0;
    for (len=0; ind!=NULL; len++) // finds num of items in list
        ind=ind->next;

    int arr[len];
    int newarr[len];
    node_t* cur= L->head;

    for(int i=0; cur!=NULL; i++){ // creates array with list items
        arr[i]=cur->data;
        cur=cur->next;
    }

    int first=0;
    int last=len;
    int leftpos=0;
    int rightpos=mid+1;
    int newpos=0;


    //  insert elements to arr until a half of the array
    //  reaches mid or last
    while(leftpos<=mid && rightpos<=last-1){
        if(arr[leftpos]<arr[rightpos]){
            newarr[newpos++]=arr[leftpos++];
        }
        else
            newarr[newpos++]=arr[rightpos++];
    }

    // fills in the rest of the array
    while(leftpos<=mid)
        newarr[newpos++]=arr[leftpos++];
    while(rightpos<=last)
        newarr[newpos++]=arr[leftpos++];
    for(int j=0; j<len; j++)
        printf("newarr=%d\n",newarr[j]); 

    }

int main(void){
    int num_nodes = 4;
    int num_nodes2 = 3;
    LL_t* L=createList(num_nodes);
    LL_t* L2=createList(num_nodes2);
    merge(L, L2);

}
// Creates the list. No problem here
LL_t* createList(int num_nodes){
    LL_t* L = malloc(sizeof(LL_t));
    L->head=NULL;
    L->tail=NULL;
    node_t *n;
    int i=0;
    for (i = 0; i < num_nodes; i++) {
        n = malloc(sizeof(*n));
        scanf("%d",&n->data);
        n->next = NULL;
        if (L->head == NULL){
            L->head = n;
            L->tail = n;
        } 
        else {
            L->tail->next = n;
            L->tail = n;
        }
    }
    puts("\n");
    return L;
    }

最佳答案

问题出在merge() :

while(rightpos<=last)
    newarr[newpos++]=arr[leftpos++];
                         ^^^^^^^

在这里,while 的条件循环是 rightpos<=last但正在访问 leftpos arr 的索引元素并增加它。如果rightpos小于 last然后是 while循环条件永远是 true这使它成为一个无限循环。 while 的每次迭代循环递增 leftpos ,在某个阶段它的值将大于数组的大小 arr访问超出数组大小的数组元素是 undefined behavior其中包括程序可能会出现段错误。应该是:

while(rightpos<=last)
    newarr[newpos++]=arr[rightpos++];

关于c - 为什么我的代码会给我垃圾值,有时还会出现段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49442726/

相关文章:

c - pthread_join() 段错误

c - (linux) "Segmentation fault (core dumped)"将 char 指针传递给 c 中的函数

c - 无法右对齐马里奥问题集的金字塔

c - 布林带段错误

c - Linux C Shell,子进程抛出段错误

c++ - segmentation 故障的常见原因的明确列表

c - 访问函数中的结构元素

c - 当 CPU 频率可变时,基于时钟的计时是否可靠?

c - 如何定义系统()

c - MPI 运行时错误 : Either Scatterv count error, segmentationfault,或卡住