c - 使用 malloc 的段错误,用于合并排序的结构数组

标签 c arrays malloc structure mergesort

此代码尝试对结构数组应用合并排序并对其进行排序。合并排序函数中存在段错误,我无法预测原因。我也附上了输出,如果需要任何东西,请告诉我。

 #include<stdio.h>
#include<stdlib.h>

typedef struct node node;

struct node{
int index; // record-key
char fname[20], lname[20], dob[20], location[20], dept[20];
}demo,delta;


node delta ={-1,'a','a','a','a','a'};

void merge(node [], int, int, int );
void mergesort(node [], int , int );

void mergesort(node a[], int start, int end){

    int mid = (start+end)/2;
    if(start<end){
        mergesort(a,start,mid);
        mergesort(a,mid+1,end);
        merge(a,start,mid,end);
    }
}

void merge(node a[], int start, int mid, int end){
    node *b = malloc(sizeof(demo)*end*2);  //creating two arrays 
    node *c = malloc(sizeof(demo)*end*2);
    int j=0,k=0,i=0,e=0,f=0;

    for(i=start;i<mid+1;i++){   //storing first half of main array into b
        b[i] = a[i];
    }
    b[i+1]=delta;    //this is a check value used for checking the length in a loop below 
    for(k=mid+1;k<end+1;k++){   //storing second part of main array into c
        c[j]=a[k];
        j++;
    }
    c[j+1]=delta;  //this is just a check value to check length

    for(i=start;i<end+1;i++){
        if(b[e].index==delta.index){    //if array b ends
            while(c[f].index!=delta.index){
                a[i]=c[f];               //copy remaining contents of c into a
                i++;
                f++;
            }
            break;
        }
        if(c[f].index==delta.index){             //if c ends
            while(b[e].index!=delta.index){    //copy remaining contents of b into a
                a[i]=b[e];
                i++;
                e++;
            }
            break;
        }

        if(b[e].index>c[f].index){    //compring first element of both b and c and overriding smaller value in main array
            a[i]=c[f];
            f++;
        }
        else if(b[e].index<=c[f].index){     //compring first element of both b and c and overriding smaller value in main array
            a[i]=b[e];
            e++;
        }
    }

    for(i=0;i<end+1;i++){
        printf("arr index: %d \n",a[i].index);    //just for checking
    }

}



int main(int argc ,char *argv[]){
    printf("entered in main\n");
    char temp[20];
    int i;

    FILE *fp =fopen(argv[1],"r");
    // printf("bakri\n");
    if(fp==NULL){
        printf("file not opened\n");
        exit(1);
    }
    node demo;
    printf("demo made %ld\n",sizeof(demo));
    int var = atoi(argv[2]);

    node *arr  = malloc(sizeof(demo)*var*2);
    if(arr==NULL){
        printf("arr is null\n");
        exit(2);
    }

    printf("array made\n");

    for(i=0;i<(atoi(argv[2]));i++){
        fgetc(fp);
        fscanf(fp,"%s %s %s %s %s %s",temp,arr[i].fname,arr[i].lname,arr[i].dob,arr[i].location,arr[i].dept);
        arr[i].index=atoi(temp);
    }
    fclose(fp);

    for(i=0;i<atoi(argv[2]);i++){
        printf("\n%d",arr[i].index);
    }

    int len = atoi(argv[2]);
    mergesort(arr,0,len-1);

    for(i=0;i<len;i++){
        printf("%d \n",arr[i].index);
    }

return 0;
}

输出是-

entered in main
demo made 104
array made

28053
31163
3786
33086
16355
4792
26524
21076
21367
27286arr index: 28053 
arr index: -414853824 
Segmentation fault (core dumped)

最佳答案

你分配的数组的最大索引:

node *c = malloc(sizeof(demo)*end);

end - 1。然后你有:

for(k=mid+1;k<end+1;k++){
    c[j]=a[k];
    j++;
}

您将超出数组的边界,这将导致段错误。

关于c - 使用 malloc 的段错误,用于合并排序的结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43348155/

相关文章:

android - GLES 2 函数在 Visual Studio 中不起作用

c++ - 如何创建一个 makefile 而不必在项目中包含每个源文件?

c - OpenMP 实现还原

php - 尝试从此处获取多列的平均值,但所有值都打印为 0

java - Array.binarySearch 返回负数

c# - 结构类型的固定大小数组

c - 在单行上打印动态分配的整数数组

c - 在 C 中,在结束前包含 NULL 的内存块上调用 free() 是否安全?

c - include_next 预处理器指令导致 MSVC 出现问题

c - 自动为 C 结构分配内存