Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C “如何修复”;程序接收信号SIGSEGV,分段故障。”;在尝试访问阵列时_C - Fatal编程技术网

C “如何修复”;程序接收信号SIGSEGV,分段故障。”;在尝试访问阵列时

C “如何修复”;程序接收信号SIGSEGV,分段故障。”;在尝试访问阵列时,c,C,我正在使用不同的选择枢轴的方法进行快速排序,我在代码中看不到任何问题,函数在单独测试时可以工作,但是当我将它们放在一起时,它们大部分时间都不工作 我尝试将文件移动到另一个路径,并更改访问阵列的方式 void quick_sort(uint32_t arr[], int first, int last, int pivot_opt) { int i, j; uint32_t pivot = pivot_select(arr, last, pivot_opt); i

我正在使用不同的选择枢轴的方法进行快速排序,我在代码中看不到任何问题,函数在单独测试时可以工作,但是当我将它们放在一起时,它们大部分时间都不工作

我尝试将文件移动到另一个路径,并更改访问阵列的方式

void quick_sort(uint32_t arr[], int first, int last, int pivot_opt)
{
    int i, j;
    uint32_t pivot = pivot_select(arr, last, pivot_opt);    

    i = first;
    j = last;

    do
    {
        comparations_count++;
        while (arr[i] < pivot) i++;             // Counting elements smaller than pivot

        comparations_count++;
        while (arr[j] > pivot) j--;             // Counting elements greater than pivot         

        comparations_count++;
        if (i <= j)
        {   
            exchanges_count++;
            swap(&arr[i++], &arr[j--]);         // Placing smaller elements in the left, and greater elements in the right without touching the pivot
        }

        comparations_count++;
    } while (i <= j);

    comparations_count++;
    if (first < j)
        quick_sort(arr, first, j, pivot_opt);   // Sorting smaller elements of array

    comparations_count++;
    if (i < last)
        quick_sort(arr, i, last, pivot_opt);    // Sorting greater elements of array

}

uint32_t pivot_select(uint32_t arr[], int last, int pivot_opt)
{   
    uint32_t pivot = 0;
    int random_index = 0;
    switch (pivot_opt)
    {
    case 0:     
        pivot = arr[last];                          // Choosing the pivot as the last element in the array
        break;
    case 1:     
        random_index = rand()%(last);               // Choosing the pivot as a random element of array
        pivot = arr[random_index];
        break;
    case 2:     
        pivot = median(arr, last);                  // Choosing the pivot as avg of three random indexes of the array
        break;
    default:
        break;
    }
    return pivot;
}

uint32_t median(uint32_t arr[], int n)
{
    if (n <= 3)
    {   
        return arr[0];                                                      // If the array have 3 or less elements, choose as pivot first element
    }
    else
    {
        int index[3] = {0};                                                 // Index of 3 elements of original array
        int last_index = 0;                                                 // Last chosen index, to verify if index was selected
        int i = 0;

        while(i < 3)                                                        // Selecting 3 random index
        {
            int current_index = (rand()%(n));
            if (current_index == last_index)    
                i--;
            else
            {
                index[i++] = current_index;
                last_index = current_index;
            }           
        }

        uint32_t array[3] = {arr[index[0]], arr[index[1]], arr[index[2]]};  // Creating array with the elements on random indexes
        insertion_sort(array, 3);                                           // Sorting the array
        return array[1];                                                    // Returning the pivot as the middle element of array
    }

}
void快速排序(uint32\u t arr[],int first,int last,int pivot\u opt)
{
int i,j;
uint32\u t pivot=pivot\u select(arr、last、pivot\u opt);
i=第一;
j=最后一个;
做
{
比较计数++;
while(arr[i]pivot)j--;//计算大于pivot的元素
比较计数++;

如果(i让我们从这个开始:

while (arr[i] < pivot) i++;
如果所有元素都大于pivot,那么j将超出范围(负数),请在此处更改条件

根据我的意见,上述领域正在造成问题


调试愉快!

您能给我们看一下测试代码吗?特别是,我们有机会看到arr、first和last的值吗?要解决这样的问题,您可以使用gdb之类的调试器,它可以告诉您何时发生分段以及变量中包含哪些值。您还可以尝试使用
printf()来“调试您自己”
要确定错误的位置及其发生的价值哇,请查看所有类似的“相关”错误右边边栏中的问题。这些问题有没有给你一些调试的想法?我在帖子中添加了测试代码,关于相关的问题,我已经读到我正在试图修改只读内存。但我认为不是这样,因为如果是这样的话,函数中值应该会给出独立的问题,而不是pivot_select funct中的问题当代码< >代码> > LSTTySt==0</代码>时,我没有考虑前两个,只需要一个小的修复,而不是=,它需要< /> CUZ,如果它在结尾或已经开始,它将增加/减少I和J,然后它会越界。关于交换(ARR[I++],和ARR[J-])没问题,因为这相当于交换(&arr[i],&arr[j]);i++;j--;@Leonard,是的,我误解了最后一个。编辑了答案。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/time.h>
#include <time.h>
#define size 1000

uint32_t comparations_count;
uint32_t exchanges_count;
int main(int argc, char** argv)
{   
    uint32_t array[size];

    fill(array, size);
    permute_array(array, size);

    comparations_count = 0;
    exchanges_count = 0;
    quick_sort(array, 0, size-1, 2);

    return 0;
}

void fill(uint32_t arr[], uint32_t n)
{       
    for (size_t i = 0; i < n; i++)              // Filling the array in ascending order
        arr[i] = i;
}

void swap(uint32_t *a, uint32_t *b)
{
    // Swapping two elements
    uint32_t t = *a;
    *a = *b;
    *b = t;
}

void permute_array(int a[], size_t n)
{
    // Adapted from:
    // https://www.geeksforgeeks.org/shuffle-a-given-array-using-fisher-yates-shuffle-algorithm/

    srand(time(NULL));                          // Init random seed

    for (size_t i = n - 1; i > 0; i--)          // Permute array
    {        
        size_t j = rand() % (i+1);              // Pick a random index from 0 to i        
        swap(&a[i], &a[j]);                     // Swap arr[i] with the element at random index
    }
}

void insertion_sort(uint32_t arr[], size_t n)
{
    uint32_t current_index = 0;
    uint32_t current_value = 0;

    for (size_t i = 1; i < n; i++) {
        current_index = i;
        current_value = arr[i];
        while (current_index > 0) {
            if (current_value < arr[current_index - 1]) {
               swap(&arr[current_index], &arr[current_index-1]);
               current_index--;
            }else { break; }            
        }     
    }
}
while (arr[i] < pivot) i++;
while (arr[j] > pivot) j--;