简体   繁体   English

为什么我的代码给我乱码,有时出现分段错误?

[英]Why is my code giving me garbage values and sometimes segmentation fault?

I'm trying to make an implementation of merge() given 2 sorted linked lists, and output a linked list that merges the two and is sorted. 我正在尝试给出给定2个排序的链表的merge()的实现,并输出将两者合并并排序的链表。 I first create an array and put the items there, but when I print the array elements I get garbage values/seg fault. 我首先创建一个数组,然后将项目放置在那里,但是当我打印数组元素时,我得到了垃圾值/段错误。 I know it's a lot of code to go through but I'd really appreciate anyone who can help :) 我知道要处理的代码很多,但我非常感谢任何可以提供帮助的人:)

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;
    }

The problem is here in the merge() : 问题在merge()

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

Here, the condition of while loop is rightpos<=last but accessing leftpos index element of arr and incrementing it. 在这里, while循环的条件是rightpos<=last但是访问arr leftpos索引元素并将其递增。 And if rightpos is less than last then the while loop condition will always be true which makes it an infinite loop. 如果rightpos小于lastwhile循环条件将始终为true ,这将使其成为无限循环。 Every iteration of while loop is incrementing leftpos , at one stage its value will be greater than the size of array arr and accessing the array element beyond the size of the array is undefined behavior which includes program may give segmentation fault. while循环的每次迭代都会向leftpos递增,在某一阶段其值将大于数组arr的大小,并且超出数组大小访问数组元素是未定义的行为 ,其中包括程序可能会导致分段错误。 It should be: 它应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM