简体   繁体   中英

how to solve segmentation error in merge sort c program?

It reports segmentation error while running, there are no other errors in this program. The error reported is:

timeout: the monitored command dumped core  
sh: line 1: 18689 Segmentation fault timeout 10s main

When I remove one of the recursive function, the program runs but the output shown is some memory addresses. I've created this program after reading merge sort program in geeksforgeeks.org. Here is the link to that program: https://www.geeksforgeeks.org/merge-sort/

#include<stdlib.h>
#include<stdio.h>
void mergesort(int arr[],int l,int m,int r)
{
    int l1=m-l+1;
    int l2=r-m;
    int arr1[l1],arr2[l2],i=0,j=0,k=1;
    for(i=0;i<l1;i++)
    {
        arr1[i]=arr[l+1];
    }
    for(j=0;j<l2;j++)
    {
        arr2[j]=arr[m+1+j];
    }
    i=0;
    j=0;
    while(i<l1&&j<l2)
    {
        if(arr1[i]<arr2[j])
        {
            arr[k]=arr1[i];
        }
        else
        {
            arr[k]=arr2[j];
        }
        k++;
    }
    while(i<l1)
    {
        arr[k]=arr1[i];
        k++;
        i++;
    }
    while(j<l2)
    {
        arr[k]=arr2[j];
        k++;
        j++;
    }
}
void printarray(int arr[],int r)
{
    printf("the array is");
    for(int i=0;i<r;i++)
    {
        printf("%d",arr[i]);
    }

}
void merge(int arr[],int l,int r)
{
    if(l<r)
    {
        int m=l+(r-1)/2;
        merge(arr,0,m);
        merge(arr,m+1,r);
        mergesort(arr,l,m,r);
    }
}
void main()
{
    int arr[]={20,10,5,60,3,40,36};
    int r=sizeof(arr)/sizeof(arr[0]);
    printarray(arr,r);
    merge(arr,0,r-1);
    printarray(arr,r);
}

This error occurs because of an out-of-scope array index that is causing a buffer overflow, an incorrectly initialised pointer, etc.

A signal is generated when a program either tries to read or write outside the memory that is allocated for it or to write memory that can only be read.

For example, you are accessing a[-1] in a language that does not support negative indices for an array.

Your code had mistakes in initialisation of variables(used to represent array index in this case) and logical errors.

Kindly go through the corrected code and comments representing specifically where you did the mistakes :

#include<stdlib.h>
#include<stdio.h>
void mergesort(int arr[],int l,int m,int r)
{
    int l1=m-l+1;
    int l2=r-m;
    int arr1[l1],arr2[l2],i=0,j=0;

    int k=l; //Initialize k to l and not to 1          

    for(i=0;i<l1;i++)
    {
        arr1[i]=arr[l+i];
    }
    for(j=0;j<l2;j++)
    {
        arr2[j]=arr[m+1+j];
    }
    i=0;
    j=0;
    while(i<l1 && j<l2)
    {
        if(arr1[i] <= arr2[j])
        {
            arr[k]=arr1[i];
            i++;  //Logical error 
        }
        else
        {
            arr[k]=arr2[j];
            j++;   //Logical error
        }
        k++;
    }
    while(i<l1)
    {
        arr[k]=arr1[i];
        k++;
        i++;
    }
    while(j<l2)
    {
        arr[k]=arr2[j];
        k++;
        j++;
    }
}
void printarray(int arr[],int r)
{
    printf("the array is");
    for(int i=0;i<r;i++)
    {
        printf("%d ",arr[i]);
    }

}
void merge(int arr[],int l,int r)
{
    if(l<r)
    {
        int m=l+(r-l)/2;    //(r-1)/2 changed to (r-l)/2
        merge(arr,l,m);
        merge(arr,m+1,r);
        mergesort(arr,al,m,r);
    }
}
void main()
{
    int arr[]={20,10,5,60,3,40,36};
    int r=sizeof(arr)/sizeof(arr[0]);
    printarray(arr,r);
    merge(arr,0,r-1);
    printarray(arr,r);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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