在 C 中创建大型数组时出现分段错误 [英] Segmentation Fault While Creating Large Arrays in C

查看:27
本文介绍了在 C 中创建大型数组时出现分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你们用这段代码帮了我很多.让我先说我不太了解 C,并且正在努力做到这一点.

you guys have helped me so much with this code. Let me preface by saying I do not know C very well and am trying really hard to do this.

这是程序应该做的:

  1. 创建一个长度为 1000 万的随机数列表
  2. 使用 shell 排序函数对随机数列表进行排序(仍然无法正常工作......我认为这是我将指针传递给函数的方式)
  3. 将列表延长 100 万
  4. 在记录时间时最多重复 1 亿次(由于某种原因,时间显示为 0.0000000)

我只是想测试这个 shell 排序程序与标准库中内置的快速排序程序.

I'm just trying to test this shell sort program vs quick sort built into the standard library.

我尝试过使用和不使用指针.注释掉的部分在完成后应该可以工作.它只会把事情搞得一团糟,哈哈

I've tried with and without pointers. The commented out section should work when it's done. It just messes things up more lol

请帮帮我,你们到目前为止都很棒...

Please help me out, you guys have been so great so far...

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


void shellSort(int *A, int n);
void checkSort(int *A, int n);

int main(){

    /*Initialize Random Array*/
    int unsorted_list[10000000];
    int *ptr = &unsorted_list[0];
    int random_number;
    int i;

    srand ( time(NULL) );
    for(i=0; i<10000000; i++){

        random_number = rand();
        unsorted_list[i] = random_number % 10000000;
    }

    //Do C Shell Sort
    double shell_results[10][2];

    double clock_diff;
    int j=10000000;
    clock_t t0, t1;
    int k;


    for(i=0;i<10;i++){



        /*Sort the list using shellSort and take the time difference*/
        t0 = clock();
        shellSort(ptr, j);
        t1= clock();

        /*Take difference in time*/
        clock_diff = (t1 - t0)/CLOCKS_PER_SEC;

        /*Add time and list length to the results array*/
        shell_results[i][0] = (double)j;
        shell_results[i][1] = clock_diff;


        /*Check to make sure the array has been sorted*/
        checkSort(ptr, j);

        /*Re-initialize a longer array*/
        //j+=1000000;
        //for(k=0; k<j; k++){
        //    random_number = rand();
        //    unsorted_list[k] = random_number % 1000000;
        //}

        printf("%d",(int)shell_results[i][0]);
        printf(" ");
        printf("%f",shell_results[i][1]);
        printf("\n");

    }





 return 0;
 }

 void shellSort(int *A, int n){



     int gap , i , j , temp;

     for (gap = n/2; gap>0; gap /=2)
         for (i=gap; i<n; i++)
             for(j = i-gap; j>=0 && A[j] > A[j+gap]; j-=gap){
                 temp = A[j];
                 A[j] = A[j + gap];
                 A[j + gap] = temp;
     }
 }



void checkSort(int *A, int n){

    int i;

    for(i=0;i<n;i++){

        if(A[i]>A[i+1]){

            printf("Error in sorting \n");
            break;
        }
    }
}

推荐答案

您可能没有 10 MB 的堆栈空间.将该数组设为全局,使用 static 声明它,或者使用 malloc() 动态分配它.如果你选择后者,不要忘记free()它.

You probably don't have 10 megabytes of stack space. Make that array global, declare it with static, or allocate it dynamically using malloc(). If you choose the latter, don't forget to free() it.

以后需要使用100,000,000个元素的数组时,请务必为其使用新的分配!

Later, when you need to use the 100,000,000 element array, make sure to use a new allocation for it!

这篇关于在 C 中创建大型数组时出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆