C:仅对大文件进行合并排序时出现段错误

标签 c sorting memory segmentation-fault mergesort

以下代码对单词数组进行排序,处理小数组,处理大数组(>400000 个单词,但我还没有找到限制)。它被一个程序调用,该程序将一组单词(从文件中读取)传递给它以进行排序并测试其是否成功:

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

#include "csort.h"
#include "sort.h"

// array points to array of pointers to strings, count is number of entries in array

void sortC(char** array, unsigned int count){
  array = merge_sort(array, count);
  // testing:
  /*for (int i = 0; i < count; i++){
    printf("%s ", array[i]);
    }*/
}

char** merge_sort(char** array, int count){
  if (count <= 1) return array;
  else {
    int lcount = 0;
    int rcount = 0;
    int middle = count/2;
    lcount = middle;
    char* left[lcount];
    subArray(array, left, 0, middle);
    rcount = count-middle;
    char* right[rcount];
    subArray(array, right, middle, count);
    return merge(merge_sort(left, lcount), merge_sort(right, rcount), array, 0, lcount, rcount);
  }
}

void subArray(char** array, char** subarray, int start, int end){
  int ai; // index in original array
  int si; // index in subarray
  for (ai = start, si = 0; ai < end; ai++, si++){
    subarray[si] = array[ai];
  }
}

char** merge(char** left, char** right, char** output, int oi, int lcount, int rcount){
  if (lcount > 0 && rcount > 0){
    int lmin = findMinimum(left, lcount);
    int rmin = findMinimum(right, rcount);
    if (strcmp(left[lmin], right[rmin]) < 0){
      output[oi] = left[lmin];
      removeFromArray(left, lmin, lcount);
      lcount--;
    }
    else {
      output[oi] = right[rmin];
      removeFromArray(right, rmin, rcount);
      rcount--;
    }
  }
  else if (lcount == 0) {
    if (rcount == 1) {
      output[oi] = right[0];
      return output;
    } else {
      int rmin = findMinimum(right, rcount);
      output[oi] = right[rmin];
      removeFromArray(right, rmin, rcount);
      rcount--;
    }
  }
  else if (rcount == 0) {
    if (lcount == 1) {
      output[oi] = left[0];
      return output;
    } else {
      int lmin = findMinimum(left, lcount);
      output[oi] = left[lmin];
      removeFromArray(left, lmin, lcount);
      lcount--;
    }
  }
  return merge(left, right, output, ++oi, lcount, rcount);
}

int findMinimum(char** array, int count){
  char* minvalue = array[0];
  char* currentvalue = minvalue;
  int minindex = 0;
  for (int i = 1; i < count; i++){
    currentvalue = array[i];
    if (strcmp(currentvalue, minvalue) < 0){
      minvalue = currentvalue;
      minindex = i;
    }
  }
  return minindex;
}

void removeFromArray(char** array, int index, int count){
  // removes specified index from an array
  for (int i = index; i < count; i++){
    if (i+1 == count){
      array[i] = 0; // this entry will be gone when count decrements
    } else {
      array[i] = array[i+1];
    }
  }
}

最佳答案

如果您的代码没有错误,那么问题可能出在您存储数据的方式上。您是使用 malloc() 分配数组来存储您的数据,还是声明一个足够大的数组?

对于大型数据集,您必须使用 malloc(),它将在 HEAP 而不是堆栈上分配空间。 堆栈空间有限。这可以解释为什么您的程序可以在较小的数据下工作,而在较大的数据集下会崩溃。

另外一个非常重要的一点是您正在使用递归:merge() 调用 merge()。过多的递归调用可能会导致堆栈溢出(段错误)。

关于C:仅对大文件进行合并排序时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5747413/

相关文章:

c - 为什么在尝试填充结构时出现段错误(核心转储)或总线错误(核心转储)?

c - 为什么 scanf 不能处理我的字符串,而是不会停止到分隔符并创建一堆数字和字母,这些数字和字母变为空?

javascript - 为什么我无法按 2 个字段的差异对列表进行排序?

hadoop - EMR-Mapreduce内存错误

c++ - 指针变量的地址

c - 如何访问已知内存地址的内容?

c - C 中的双向链表排序

java - 什么决定比较器/可比较集合类中的升序或降序?

c++ - 奇数链表表示

改变数组的基址