如何声明和在C使用1十亿巨额的整数数组? [英] How to declare and use huge arrays of 1 billion integers in C?
问题描述
我执行一个顺序程序进行排序像快速排序。我想在一个巨大的1或10亿元整数数组来测试我的程序的性能。
但问题是,我得到的分割误差,由于该阵列的大小。
I'm implementing a sequential program for sorting like quicksort. I would like to test the performance of my program in a huge array of 1 or 10 billions of integers. But the problem is that I obtain a segmentation error due to the size of the array.
这个数组的声明的样本code:
A sample code of declaration of this array:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 1000000000
int main(int argc, char **argv)
{
int list[N], i;
srand(time(NULL));
for(i=0; i<N; i++)
list[i] = rand()%1000;
return 0;
}
我有一个主张使用mmap功能。但我不知道如何使用它?任何人可以帮助我使用它?
I got a proposition to use mmap function. But I don't know how to use it ? can anybody help me to use it ?
我工作在Ubuntu 10.04 64位,GCC 4.4.3版。
I'm working on Ubuntu 10.04 64-bit, gcc version 4.4.3.
感谢您的答复。
推荐答案
迈克尔是正确的,你可以不适合那么多堆栈。但是,你可以把它的全球(或静态),如果你不想对malloc它。
Michael is right, you can't fit that much on the stack. However, you can make it global (or static) if you don't want to malloc it.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 1000000000
int list[N];
int main(int argc, char **argv)
{
int i;
srand(time(NULL));
for(i=0; i<N; i++)
list[i] = rand()%1000;
return 0;
}
这篇关于如何声明和在C使用1十亿巨额的整数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!