当数组大小为一百万时程序崩溃

3

可能是重复问题:
C语言中大数组导致分段错误

我正在尝试使用不同的输入大小(如10,000、100,000和1,000,000)比较归并排序和快速排序。然而,当我使用一百万个输入时,程序崩溃了,我不知道为什么?另一方面,数组是通过从包含数字的文件中读取填充的,下面是我的简单归并排序。

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

#define SIZE 1000000

void Merge(int * , int , int , int );
void MergeSort(int *array, int left, int right);

int main(){

    struct timeval tv; 
    struct timezone tz; 
    struct tm *tm; 
    long start,stop;

    long i,num;
    int array[SIZE];

    FILE* fptr;

    gettimeofday(&tv, &tz); 
    tm = localtime(&tv.tv_sec); 
    printf("TIMESTAMP-START\t %d:%02d:%02d:%d (~%d ms)\n", tm->tm_hour, 
    tm->tm_min, tm->tm_sec, tv.tv_usec, 
    tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 + 
    tm->tm_sec * 1000 + tv.tv_usec / 1000);

    start = tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 + tm->tm_sec * 1000 + tv.tv_usec / 1000;

    fptr = fopen ("onemillion.txt","r");

    for(i=0; i<SIZE-1; i++){

        fscanf(fptr,"%d",&num);
        array[i]=num;
    }

    MergeSort(array,0,SIZE-1);

    /*for(i = 0;i < SIZE;i++)
        {
                printf("%d \n",array[i]);
        }*/

    gettimeofday(&tv, &tz); 
    tm = localtime(&tv.tv_sec); 
    stop = tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 + 
    tm->tm_sec * 1000 + tv.tv_usec / 1000; 
    printf("TIMESTAMP-END\t %d:%02d:%02d:%d (~%d ms) \n", tm->tm_hour, 
    tm->tm_min, tm->tm_sec, tv.tv_usec, 
    tm->tm_hour * 3600 * 1000 + tm->tm_min * 60 * 1000 + 
    tm->tm_sec * 1000 + tv.tv_usec / 1000); 

    printf("ELAPSED\t %d ms\n", stop - start);

    return 0;

}

void MergeSort(int *array, int left, int right)
{
        int mid = (left+right)/2;
        /* We have to sort only when left<right because when left=right it is anyhow sorted*/
        if(left<right)
        {
                /* Sort the left part */
                MergeSort(array,left,mid);
                /* Sort the right part */
                MergeSort(array,mid+1,right);
                /* Merge the two sorted parts */
                Merge(array,left,mid,right);
        }
}

void Merge(int *array, int left, int mid, int right)
{
        /*We need a Temporary array to store the new sorted part*/
        int tempArray[right-left+1];
        int pos=0,lpos = left,rpos = mid + 1;
        while(lpos <= mid && rpos <= right)
        {
                if(array[lpos] < array[rpos])
                {
                        tempArray[pos++] = array[lpos++];
                }
                else
                {
                        tempArray[pos++] = array[rpos++];
                }
        }
        while(lpos <= mid)  tempArray[pos++] = array[lpos++];
        while(rpos <= right)tempArray[pos++] = array[rpos++];
        int iter;
        /* Copy back the sorted array to the original array */
        for(iter = 0;iter < pos; iter++)
        {
                array[iter+left] = tempArray[iter];
        }
        return;
}

当我使用一千、一万和十万时,没有问题。正如我所说,我在处理一百万的输入大小时遇到了困难。我不确定,但我猜想这与使用数组有关?如果你能帮忙就太好了,无论如何还是谢谢。
2个回答

4

您遇到了堆栈溢出的问题。堆栈内存不足以容纳您的请求,导致运行时异常。

尝试使用malloc动态分配数组。


2

C语言非常严格,不能像其他语言那样决定数组放在哪里。它总是将这样的数组放在栈上,而栈的空间很小。请使用动态分配。


3
刚才有人说C语言非常严格吗? - user507577
这要看“strict”一词的含义。当然,您可以随意处理指针。在我使用的意义上,它并不是程序员的自由,而是编译器的自由。它只需通过自动存储分配数组即可。在其他语言中,编译器可以决定。 - Vladimir F Героям слава

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接