当数组大小为一百万程序崩溃 [英] Program crashes when array size is one million

查看:449
本文介绍了当数组大小为一百万程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  大阵给分段错误使用C

我试图来比较不同的输入大小不一样10.000,100.000和1.000.000合并排序和快速排序。然而,当我给百万输入大小的程序崩溃,我不知道是为什么?。另一方面,阵中满是包含数字,这里是我的简单归并排序文件读取。

i'm trying to compare merge sort and quick sort with different input sizes like 10.000, 100.000, and 1.000.000. However, when i give one million input size program is crashing and i don't know why ?.On the other hand, array is filled with reading from a file which contains numbers and here is my simple merge sort.

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

当我与千,万和10万尝试是没有问题的。正如我说我很难一百万输入大小。我不知道,但我想它是关于使用数组?我会很高兴,如果你能帮助和感谢反正。

when i try with one thousand, ten thousand and one hundred thousand there is no problem. As i said i struggle with one million input size. I'm not sure but i guess it is about using an array ?. I will be glad if you can help and thanks anyway.

推荐答案

您得到一个堆栈溢出。没有足够的堆栈内存来容纳导致运行时异常请求。

You are getting an stack overflow. There is not enough stack memory to accommodate your request resulting in run-time exception.

尝试动态地分配数组使用的malloc

Try allocating the array dynamically using malloc.

这篇关于当数组大小为一百万程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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