Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

Segmentation Fault While Creating Large Arrays in 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.

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 doesn't 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 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("
");

    }





 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 
");
            break;
        }
    }
}
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...