分段错误而创建用C大型阵列 [英] Segmentation Fault While Creating Large Arrays in C

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

问题描述

你们帮助了我这么多与此code。让我先说,我不知道的C非常好,我想真的很难做到这一点preface。

这是该方案应该做的:

1)创建长度1000万的随机号码的列表
2)使用排序排序壳功能(仍然不工作正常...我认为它的我是多么传递函数指针)随机数的列表
3)制作的列表1百万更长

4)重复可达100万次,同时记录时间(时间显示为0.0000000出于某种原因)

我只是想测试这个外壳排序程序VS内置标准库快速排序。

我试过与无球......当其done..It只是弄乱的东西了注释掉部分应该工作更笑

请帮我,你们有如此之大,到目前为止...

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&time.h中GT;
无效希尔(INT * A,INT N);
无效checkSort为(int * A,INT N);诠释主(){    / *初始化随机序列* /
    INT unsorted_list [千万]
    为int * ptr的=安培; unsorted_list [0];
    INT RANDOM_NUMBER;
    INT I;    函数srand(时间(NULL));
    对于(i = 0; I<千万,我++){        RANDOM_NUMBER = RAND();
        unsorted_list [I] = RANDOM_NUMBER%千万;
    }    //可做C Shell排序
    双shell_results [10] [2];    双clock_diff;
    INT J =千万;
    clock_t表示T0,T1;
    时int k;
    对于(I = 0; I&小于10;我++){        / *使用希尔排序名单,并采取时间差* /
        T0 =​​时钟();
        希尔(PTR,J);
        T1 =时钟();        / *采取时间差* /
        clock_diff =(T1 - T0)/ CLOCKS_PER_SEC;        / *添加时间和列表长度的结果数组* /
        shell_results [I] [0] =(双)焦耳;
        shell_results [I] [1] = clock_diff;
        / *检查以确保该数组已排序* /
        checkSort(PTR,J);        / *重新初始化一个较长的数组* /
        // J + = 1000000;
        //为(K = 0; K<Ĵ; 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);    } 返回0;
 } 无效希尔(INT * A,INT N){     INT差距,I,J,温度;     为(间隙= N / 2;间隙大于0;间隙/ = 2)
         对于(i =差距; I< N;我++)
             为(J = I-差距; J> = 0&放大器;&安培; A [J]>一种[J +差距]; J- =间隙){
                 温度= A [J]。
                 A [D] = A [J +差距]
                 A [J +差距] =温度;
     }
 }无效checkSort为(int * A,INT N){    INT I;    对于(i = 0; I< N;我++){        如果(A [I]>将第[i + 1]){            的printf(错误的排序\\ n);
            打破;
        }
    }
}


解决方案

您可能没有堆栈空间10兆字节。作出这样的阵列全球性的,静态申报,或者采用动态的malloc分配它()。如果选择后者,别忘了免费()了。

之后,当您需要使用100,000,000元阵列,请务必使用一个新的分配呢!

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.

This is what the program should do:

1) Create a list of random numbers of length 10 Million 2) Sort the list of random numbers using shell sort function (still doesnt work properly...i think its how I am passing the pointer to the function) 3) Make a list 1 Million Longer

4) repeat for up to 100 million while recording time (the time shows up as 0.0000000 for some reason)

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 its 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;
        }
    }


}

解决方案

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.

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天全站免登陆