簡體   English   中英

C Mergesort Segfault

[英]C Mergesort Segfault

情況

我試圖實現一個更有趣的mergesort,它創建一個隨機長度數組,隨機值,然后隨機化它們,但經過調試和編譯它的段錯誤。 我不知道為什么會出現段錯誤,但我確定它與內存分配有關。

為什么這段代碼會導致段錯?

#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而不是每次都計算它。

這似乎是問題所在:

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

你基本上return sizeof(int*)/sizeof(int) ,這不是你想要的。 這整個事情的出現是因為數組在傳遞給函數時會衰減為指針。

您應該閱讀comp.lang.c FAQ中的Arrays and Pointers部分以獲得啟發。

  1. 使用/ WALL運行程序時會發生什么? 什么警告正在吐出來? 為什么?
  2. 當您使用附加的調試器單步執行程序時會發生什么? 每行的每個變量的值是多少? 為什么?

您的代碼有幾個問題:

  1. 您不檢查malloc的結果以查看它是否返回NULL。

  2. 您正在將數組的取消引用傳遞給init_array,即您將數組的第一個int發送到init_array,然后立即取消引用它。 由於malloc返回垃圾數據,因此您將取消引用init_array中的隨機數。

  3. array_size不是魔法。 如果您沒有在C中跟蹤數組的大小,則無法回顧性地找出您希望它們有多大。 您需要記住數組的大小並將其傳遞給init_array。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM