C 合并排序段错误

标签 c algorithm sorting segmentation-fault

情况

我试图实现一个更有趣的归并排序,它创建一个具有随机值的随机长度数组,然后将它们随机化,但在调试和编译后它出现了段错误。我不知道它为什么会出现段错误,但我确定它与内存分配有关。

问题

为什么这段代码会导致段错误?

代码

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


// Declare some stuff up front

int array_size(int *array);
int print_array(int *array);

//Some decade old main function coming at you

int main() {

    //Concerned with the integrity of my rand
    srand( (unsigned)time( NULL ));

    //A global, random length array between 1 and 100?
    int *array;
    array = malloc(sizeof(*array) * ((rand() % 100) + 1));


    init_array(*array);




    getchar();
    return 0;
}

int init_array(int *array)  {
    //Base case
    array[0] = 1;
    //random values for i in array
    int i;
    for(i = 1; i <= array_size(array); i++)  {
          array[i] = rand() % array_size(array) + 1;
    }
    //randomize the random values in the random length array
    for (i = 0; i < (array_size(array) - 1); i++)
    {
        unsigned int swapA = (rand() % array_size(array)) + 1;
        int a = array[swapA];
        array[swapA] = array[i];
        array[i] = a;
    }
    //output random array, then mergeSort the array
    print_array(array);
    sort_array(array);
    return 0;
}

//Get my array.Length
int array_size(int *array) {
    return sizeof(array)/sizeof(array[0]);
}

//Output array
int print_array(int *array) {
     int i;
     for(i = 0; i < (array_size(array) + 1); i++) {
           printf("%d\n", array[i]);
     }
     return 0;
}
     //merge the array after sorting
void merge_array(int *array, int low, int split, int high)  {
     int sorted[high-low+1];
     int a = 0;
     int b = low;
     int c = split + 1;
     //iterate from beginning to middle and from middle to end in parallel
     while(b <= split && c <= high)
     {
            if(array[b] < array[c])
            {
                sorted[a++] = array[b++];
            }
            else
            {
                sorted[a++] = array[c++];
            }
     }

     while(b <= split) sorted[a++] = array[b++];
     while(c <= high)  sorted[a++] = array[c++];
     int i;
     for(i = 0; i < a; i++) {
           array[i+low] = sorted[i];
     }
     print_array(array);            //Print sorted array
}
     //Sort the array
int sort_array(int *array, int low, int high) {
    int split = ( low + high ) / 2;
    if( low < high ) {
        sort_array(array, low, split);
        sort_array(array, split + 1, high);
        merge_array(array, low, split, high);
    }

}

最佳答案

return sizeof(array)/sizeof(array[0]);

上述语句的计算结果为 1(假设 sizeof(int *) = sizeof(int),如 H2CO3 所指出的)。

尝试这样的事情,

int main() {

//Concerned with the integrity of my rand
srand( (unsigned)time( NULL ));

//A global, random length array between 1 and 100?
int *array;
int number_of_elements = (rand() % 100) + 1;
array = malloc(sizeof(*array) * num_of_elements);

init_array(*array, num_of_elements);

getchar();
return 0;

将元素的数量作为参数传递给 init_array 而不是每次都计算它。

关于C 合并排序段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14701255/

相关文章:

c - 如何将字符串类型转换为具有不同大小的 char 数组成员的结构?

c - 使用 C 程序在网络中查找三元组

SWIFT 对具有特定对象的不同对象集合的单元格进行排序

Java按有序值打印TreeMap

c - 在c程序中接收后字符串以/UN结尾?

c - 这个c代码的o/p是什么?为什么?

Javascript 掩码计算

algorithm - 动态规划求最小硬币数

algorithm - 给定一个节点网络,如何找到具有有限移动次数的最高得分循环?

arrays - VBA中的插入排序 - 不起作用