本文介绍了大数组声明时出现分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用2mil声明一个无符号的long long数组.元素.当我首先定义数组 const 的长度,然后定义数组时,出现了分段错误错误.但是,当我将长度定义为unsigned long long时,声明就起作用了.
I want to declare an unsigned long long array with 2mil. elements. When I first define the length of the array const and then define the array I get a segmentation fault error. However when I define the length just as unsigned long long the declaration works.
int main(int argc, const char *argv[])
{
const unsigned long long lim = 2000000; //If I omit const, it works.
unsigned long long nums2lim[lim];
exit(EXIT_SUCCESS);
}
有人知道为什么会引发分割错误吗?
Does anybody know why the segmentation fault is thrown?
推荐答案
自动内存(用于本地数组)的限制远低于可分配内存.
The auto memory (used in local arrays) has a limit well below the allocatable memory.
您几乎可以做到这一点(它使用指针)
You can do practically the same with this (it uses pointers)
#include <stdlib.h>
int main(int argc, const char *argv[])
{
unsigned long long lim = 2000000;
unsigned long long *nums2lim=malloc(lim*sizeof(unsigned long long));
free(nums2lim); // don't forget this!
return EXIT_SUCCESS;
}
这篇关于大数组声明时出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!